对于我来说,这是迄今所涉及的几乎每个人的意见相结合。
首先,我开始用这个简单的测试:
it "routes/to the widgets controller" do
get('/').should route_to("mozoo/widget#index")
end
这导致:
Failures:
1) Mozoo::WidgetController GET widget index routes/to the widgets controller
Failure/Error: get('/').should route_to("mozoo/widget#index")
ActionController::RoutingError:
No route matches {:controller=>"mozoo/widget", :action=>"/"}
# ./spec/controllers/mozoo/widget_controller_spec.rb:9:in `block (3 levels) in <module:Mozoo>'
所以我从get('/')
切换到{ :get => '/' }
,事情开始伟大的工作。不知道为什么。根据lib/rspec/rails/matchers/routing_matchers.rb L102-105,没有什么区别,但它对我有所不同。无论如何,谢谢@卡梅伦 - 波普。
接下来,我增加了一个非常简单,非常类似的测试与上面:
it "routes root_path to the widgets controller" do
{ :get => root_path }.should route_to("mozoo/widget#index")
end
,并收到此错误:
Failures:
1) Mozoo::WidgetController GET widget index routes root_path to the widgets controller
Failure/Error: { :get => '/mozoo' }.should route_to("mozoo/widget#index")
No route matches "/mozoo"
# ./spec/controllers/mozoo/widget_controller_spec.rb:14:in `block (3 levels) in <module:Mozoo>'
所以我加了这一点:
before(:each) { @routes = Mozoo::Engine.routes }
并得到了更好/不同的错误:
Failures:
1) Mozoo::WidgetController GET widget index routes root_path to the widgets controller
Failure/Error: { :get => root_path }.should route_to("mozoo/widget#index")
The recognized options <{"controller"=>"mozoo/widget", "action"=>"index", "section"=>"mozoo"}> did not match <{"controller"=>"mozoo/widget", "action"=>"index"}>, difference: <{"section"=>"mozoo"}>.
<{"controller"=>"mozoo/widget", "action"=>"index"}> expected but was
<{"controller"=>"mozoo/widget", "action"=>"index", "section"=>"mozoo"}>.
# ./spec/controllers/mozoo/widget_controller_spec.rb:14:in `block (3 levels) in <module:Mozoo>'
从那里,我改变了我的测试中添加的部分(命名空间不是我的引擎下):
{ :get => root_path }.should route_to(:controller => "mozoo/widget", :action => "index", :section => "mozoo")
和中提琴,它通过。谢谢@史蒂文安德森。
接下来的部分很奇怪。添加其他测试其使用的widget_path网址助手名为路径特定的控件后:
it "will successfully serve the widget show page" do
visit widget_path(:foobar)
response.should be_success
end
测试及时blowd在我身上有:
Failures:
1) GET bubble_summary_row widget will have the content section properly scoped
Failure/Error: visit widget_path(:bubble_summary_row)
NoMethodError:
undefined method `widget_path' for #<RSpec::Core::ExampleGroup::Nested_3:0x0000010748f618>
# ./spec/views/mozoo/widgets/show.html.haml_spec.rb:7:in `block (2 levels) in <module:Mozoo>'
所以我增加了以下spec_helper config条目:
RSpec.configure do |config|
config.include Testy::Engine.routes.url_helpers
end
和BAM!它通过了。谢谢@ sam-soffes。造成这种情况的奇怪之处在于,在创建此评论后,我删除了该配置条目以尝试并返回错误,并且我无法简单地通过删除配置条目来重现错误。哦,我正在继续前进。希望这个冗长的帐户可以帮助某人。
你看到的路线,当你运行rake路线? –
你是否在index_routing_spec.rb中需要'spec_helper'? – squarism
rake路径在rails 3.1引擎中不起作用。也许这是一个错误,但它在这里解释:http://stackoverflow.com/questions/7431687/listing-rake-routes-for-a-mountable-rails-3-1-engine –