对我来说,到目前为止,几乎所有参与的人都将 cmet 组合在一起。
首先,我从这个简单的测试开始:
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,没有区别,但对我来说有区别。无论如何,谢谢@cameron-pope。
接下来,我添加了另一个非常简单且与上面非常相似的测试:
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")
还有中提琴,它通过了。谢谢@steven-anderson。
接下来的部分很奇怪。在为使用 widget_path url helper 命名路由的特定小部件添加另一个测试后:
it "will successfully serve the widget show page" do
visit widget_path(:foobar)
response.should be_success
end
测试很快就让我大吃一惊:
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 配置条目:
RSpec.configure do |config|
config.include Testy::Engine.routes.url_helpers
end
还有 BAM!它通过了。谢谢@sam-soffes。奇怪的是,后来在创建此评论时,我删除了该配置条目以尝试恢复错误,但我无法仅通过删除配置条目来重现该错误。哦,好吧,我继续前进。希望这个冗长的帐户可以帮助某人。