BooBailey
BooBailey

Reputation: 540

Explain what respond_to? :should in cucumber?

I am using ruby on rails and cucumber for the first time. I am trying to define step definitions and was hope someone could explain the following code that i found in the steps definitions for me:

Then /^(?:|I )should see movies: "([^\"]*)"/ do |movie_list| #handles a text containing a text list of movies to check for
    if page.respond_to? :should
        page.should have_content(movie) #checks if movie appears on page
    else
    assert page.has_content?(text)
    end
end

My scenario is:

Scenario: all ratings selected
Given I am on the RottenPotatoes home page
Then I should see all movies

and I am trying to test if all items from the database are being displayed. I am supposed to use the .should and assert on the rows of a database. The hint I got was to assert that rows.should == value, but can't get it to work because I don't even understand it!

So upon further understanding, I produced the follow method to handle the above scenario:

Then /^I should see all movies$/ do
  count = Movie.count
  assert rows.should == count unless !(rows.respond_to? :should)
end

but cucumber is still failing that scenario. suggestion?

Upvotes: 1

Views: 1255

Answers (2)

muttonlamb
muttonlamb

Reputation: 6501

Ok, in your steps, your Given should tell capybara to visit the correct page

Such as

Given ....
  visit '/url for movies'

This will visit the url and load the page into the page variable

Then /^(?:|I )should see movies: "([^\"]*)"/ do |movie_list| #handles a text containing a text list of movies to check for
    page.should have_content 'something that you are looking for'
end

If you are working with a blank database, i.e. no movies in it, you will need to define a movie in the Given block.

There is a very good introduction here http://ruby.railstutorial.org/chapters/sign-in-sign-out#sec-cucumber

Upvotes: 0

pat
pat

Reputation: 16226

Here's an explanation, line by line:

Then /^(?:|I )should see movies: "([^\"]*)"/ do |movie_list| #handles a text containing a text list of movies to check for
  # Does page respond to the should method? If it does, RSpec is loaded, and
  # we can use the methods from it (especially the should method, but
  # sometimes others)
  if page.respond_to? :should
    # Does the page contain the string that the movie variable refers to?
    # I'm not sure where this variable is being defined - perhaps you mean
    # movie_list?
    page.should have_content(movie) #checks if movie appears on page
  else
    # RSpec is not installed, so let's use TestUnit syntax instead, checking
    # the same thing - but it's checking against the variable 'text' - movie_list
    # again? Or something else?
    assert page.has_content?(text)
  end
end

With all that in mind - when you're writing your own steps, just use the RSpec approach (if you're using RSpec) or the TestUnit approach. You don't need if page.respond_to? :should everywhere in your step definitions.

Upvotes: 4

Related Questions