Search code examples
ruby-on-railscucumberscenarios

Not able to populate db to run cucumber scenarios


I'm learning Cucumber, and I have to populate the db to run the scenarios.

These are the instructions:

(...) 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.

This one of the *.feature files

Feature: display list of movies filtered by MPAA rating
 
     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  |

This is my code from *_steps.rb:

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

And this is the error I get:

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:'

I have tried movie = Movie.create!, Movie.create!(movie), Movie.create! movie, movie = Movie.create! (this last one just for pure desperation)... What am I doing wrong?


Solution

  • Looks good to me.

    You iterate over the movies and then just before the end you do fail "Unimplemented". What would you expect?