以下是spec / controllers / movies_controller_spec.rb中我的rspec代码的一部分:
it 'should call a controller method to receive the click on
"Find With Same Director", and grab the id of the subject' do
Movie.stub('find_similar_movies')
controller.should_receive('similar_movies').
with(hash_including :id => '3')
visit movie_path('3')
click_link('Find Movies With Same Director')
end
我总是得到错误:
Failure/Error: click_link('Find Movies With Same Director')
Capybara::ElementNotFound:
Unable to find link "Find Movies With Same Director"
以下是我在app / views / movies / show.html.haml中的观点的一部分:
%h2 Details about #{@movie.title}
%ul#details
%li
Rating:
= @movie.rating
%li
Director:
= @movie.director
%li
Released on:
= @movie.release_date.strftime("%B %d, %Y")
%h3 Description:
%p#description= @movie.description
= link_to 'Edit', edit_movie_path(@movie)
= button_to 'Delete', movie_path(@movie), :method => :delete, :confirm => 'Are you sure?'
= link_to 'Back to movie list', movies_path
= link_to 'Find Movies With Same Director', similar_movies_path(@movie)
我也用同样的步骤运行黄瓜:访问同一页面,然后点击链接,它就通过了。但是在rspec中,我没有通过测试。
答案 0 :(得分:1)
默认情况下,视图不会在RSpec控制器测试中呈现,因此您的Capybara find_link
方法正在查看空白并且无法成功。
您可以通过在render_views
块顶部describe
调用{{1}}来“打开”示例组的视图呈现,如https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/render-views
但是,控制器应使用其他机制进行测试,如https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs
中所述另见How do I test whether my controller rendered the right layout on Rails 3?。