1

我第一次在铁轨和黄瓜上使用红宝石。我正在尝试定义步骤定义,并希望有人可以为我解释在步骤定义中找到的以下代码:

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

我的情况是:

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

我正在尝试测试是否正在显示数据库中的所有项目。我应该在数据库的行上使用.shouldand 。assert我得到的提示是assertrows.should == value但无法让它工作,因为我什至不明白!

所以在进一步了解后,我产生了以下方法来处理上述情况:

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

但是黄瓜仍然没有解决这种情况。建议?

4

2 回答 2

4

这是逐行解释:

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

考虑到所有这些 - 当您编写自己的步骤时,只需使用 RSpec 方法(如果您正在使用 RSpec)TestUnit 方法。您不需要if page.respond_to? :should在步骤定义中的任何地方。

于 2013-04-05T01:27:58.617 回答
0

好的,在您的步骤中,您的 Given 应该告诉 capybara 访问正确的页面

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

这将访问 url 并将页面加载到 page 变量中

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

如果您使用的是空白数据库,即其中没有电影,则需要在 Given 块中定义电影。

这里有一个很好的介绍http://ruby.railstutorial.org/chapters/sign-in-sign-out#sec-cucumber

于 2013-04-05T01:30:05.893 回答