Rspec 视图:使用错误的控制器名称没有路由匹配
Rspec Views : No route matches using wrong controller name
我想测试一本书。
我通过 http://..../books/1
在我的浏览器中呈现它
这是我在 运行 Rspec:
时得到的错误
1) books/show should display the book name
Failure/Error: render
ActionView::Template::Error:
No route matches {:action=>"show", :controller=>"books"}
这是我使用 Factory Girl 进行的测试:
RSpec.describe "books/show", type: :view do
before(:each) do
@book = FactoryGirl.create(:book)
render
end
it "should display the book name" do
rendered.should contain(@book.name)
end
end
我想我发现了错误,但我不明白。
在我的 routes.rb
文件中我有 resources :books
如果我在我的控制台中执行 rake routes,我得到:
...
books GET /books(.:format) books#index
POST /books(.:format) books#create
new_book GET /books/new(.:format) books#new
edit_book GET /books/:id/edit(.:format) books#edit
book GET /books/:id(.:format) books#show
PATCH /books/:id(.:format) books#update
PUT /books/:id(.:format) books#update
DELETE /books/:id(.:format) books#destroy
...
为什么 book
和 bookS
之间的路线不同?我想这就是引发错误的原因。
show 动作仅适用于控制器 book
,不适用 bookS
。
books
控制器确实与 book
控制器不同。
您的路线表明您在书籍控制器中拥有传统的 show
路线,但请注意,它需要存在 id
参数。您的模板必须尝试在 /books
上通过 get
操作调用 show
操作,但未指定 ID。
如果您出示模板代码,我们可以确认。
正如@PeterAlfvin 所建议的,我认为是错误。
在我的控制器中,我设置了一些与 book
相关的变量,但我假设(现在我明白为什么不是)控制器在测试视图期间被调用并且应该在测试期间设置这些变量.
因此,对于在我的视图中调用的每个变量,我创建了一个分配行,现在一切正常。
assign(:my_variable, MY_VALUE)
最初的错误是因为我用 nil
URL.
渲染了 link_to
我想测试一本书。
我通过 http://..../books/1
这是我在 运行 Rspec:
时得到的错误1) books/show should display the book name
Failure/Error: render
ActionView::Template::Error:
No route matches {:action=>"show", :controller=>"books"}
这是我使用 Factory Girl 进行的测试:
RSpec.describe "books/show", type: :view do
before(:each) do
@book = FactoryGirl.create(:book)
render
end
it "should display the book name" do
rendered.should contain(@book.name)
end
end
我想我发现了错误,但我不明白。
在我的 routes.rb
文件中我有 resources :books
如果我在我的控制台中执行 rake routes,我得到:
...
books GET /books(.:format) books#index
POST /books(.:format) books#create
new_book GET /books/new(.:format) books#new
edit_book GET /books/:id/edit(.:format) books#edit
book GET /books/:id(.:format) books#show
PATCH /books/:id(.:format) books#update
PUT /books/:id(.:format) books#update
DELETE /books/:id(.:format) books#destroy
...
为什么 book
和 bookS
之间的路线不同?我想这就是引发错误的原因。
show 动作仅适用于控制器 book
,不适用 bookS
。
books
控制器确实与 book
控制器不同。
您的路线表明您在书籍控制器中拥有传统的 show
路线,但请注意,它需要存在 id
参数。您的模板必须尝试在 /books
上通过 get
操作调用 show
操作,但未指定 ID。
如果您出示模板代码,我们可以确认。
正如@PeterAlfvin 所建议的,我认为是错误。
在我的控制器中,我设置了一些与 book
相关的变量,但我假设(现在我明白为什么不是)控制器在测试视图期间被调用并且应该在测试期间设置这些变量.
因此,对于在我的视图中调用的每个变量,我创建了一个分配行,现在一切正常。
assign(:my_variable, MY_VALUE)
最初的错误是因为我用 nil
URL.
link_to