【发布时间】:2015-08-16 13:37:54
【问题描述】:
我有一本想要测试的书的视图。
我通过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 路由,我会得到:
...
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。
【问题讨论】:
-
试试
render @book或render book_path(@book)让我知道:) -
@ArupRakshit 没有更多的成功:(
标签: ruby-on-rails rspec routes