Reputation: 23
Here is part of my rspec code in spec/controllers/movies_controller_spec.rb:
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
and I always get the error:
Failure/Error: click_link('Find Movies With Same Director')
Capybara::ElementNotFound:
Unable to find link "Find Movies With Same Director"
Here is part of my view in 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)
I also run the cucumber with the same step: visit the same page, and click the link, it passed. But in the rspec, I failed the test.
Upvotes: 0
Views: 1109
Reputation: 29409
By default, views are not rendered in RSpec controller tests, so your Capybara find_link
method is looking through emptiness and cannot succeed.
You can "turn on" view rendering for an example group by including a call to render_views
at the top of the describe
block as discussed in https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/render-views
However, controllers are intended to be tested with other mechanisms, as discussed in https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs
See also How do I test whether my controller rendered the right layout on Rails 3?.
Upvotes: 1