无法将数据库填充到 运行 黄瓜场景

Not able to populate db to run cucumber scenarios

我被这个问题困扰了一段时间,我一直在搜索,但我无法弄清楚为什么我的代码不起作用...

我正在学习 Cucumber,我必须将数据库填充到 运行 场景。

这些是说明:

(...) you will create a step definition that will match the step Given the following movies exist in the Background section of both sort_movie_list.feature and filter_movie_list.feature. (Later in the course, we will show how to DRY out the repeated Background sections in the two feature files.)

Add your code in the movie_steps.rb step definition file. You can just use ActiveRecord calls to directly add movies to the database; it`s OK to bypass the GUI associated with creating new movies, since that's not what these scenarios are testing.

这是 *.feature 文件中的一个

功能:显示按 MPAA 分级过滤的电影列表

 As a concerned parent
  So that I can quickly browse movies appropriate for my family
  I want to see movies matching only certain MPAA ratings

Background: movies have been added to database

  Given the following movies exist:
  | title                   | rating | release_date |
  | Aladdin                 | G      | 25-Nov-1992  |
  | The Terminator          | R      | 26-Oct-1984  |
  | When Harry Met Sally    | R      | 21-Jul-1989  |
  | The Help                | PG-13  | 10-Aug-2011  |
  | Chocolat                | PG-13  | 5-Jan-2001   |
  | Amelie                  | R      | 25-Apr-2001  |
  | 2001: A Space Odyssey   | G      | 6-Apr-1968   |
  | The Incredibles         | PG     | 5-Nov-2004   |
  | Raiders of the Lost Ark | PG     | 12-Jun-1981  |
  | Chicken Run             | G      | 21-Jun-2000  |

所以这是我来自 *_steps.rb 的代码:

Given /the following movies exist/ do |movies_table|
  movies_table.hashes.each do |movie|
    Movie.create!(movie)
  end
  fail "Unimplemented"
end

这是我得到的错误:

Background: movies have been added to database # features/sort_movie_list.feature:7
    Given the following movies exist:            # features/step_definitions/movie_steps.rb:3
      | title                   | rating | release_date |
      | Aladdin                 | G      | 25-Nov-1992  |
      | The Terminator          | R      | 26-Oct-1984  |
      | When Harry Met Sally    | R      | 21-Jul-1989  |
      | The Help                | PG-13  | 10-Aug-2011  |
      | Chocolat                | PG-13  | 5-Jan-2001   |
      | Amelie                  | R      | 25-Apr-2001  |
      | 2001: A Space Odyssey   | G      | 6-Apr-1968   |
      | The Incredibles         | PG     | 5-Nov-2004   |
      | Raiders of the Lost Ark | PG     | 12-Jun-1981  |
      | Chicken Run             | G      | 21-Jun-2000  |
      Unimplemented (RuntimeError)
      ./features/step_definitions/movie_steps.rb:7:in `/the following movies exist/'
      features/sort_movie_list.feature:9:in `Given the following movies exist:'

我试过了movie = Movie.create!, Movie.create!(movie), Movie.create! movie, movie = Movie.create!(最后一个只是为了纯粹的绝望)...我做错了什么??

提前致谢:)

我觉得不错。

您遍历电影,然后在 end 之前 fail "Unimplemented"。你会期待什么?