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 にアクセスし、ページがページ変数に読み込まれます。

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 に答える