2016-07-11 96 views
1

我一直堅持用這樣的一段時間,我一直在尋找,但我無法弄清楚,爲什麼我的代碼不工作...無法填充數據庫運行黃瓜場景

我學習Cucumber,我必須填充數據庫來運行場景。

這些都說明:

(......),你將創建一個步驟定義,將匹配在這兩個 sort_movie_list.featurefilter_movie_list.featureBackground部分步驟 Given the following movies exist。 (後來在 過程中,我們將介紹如何幹出重複Background 部分在兩個特徵文件。)

加入你的代碼中movie_steps.rb步驟定義文件。你可以用 只使用ActiveRecord調用來直接添加電影到數據庫; 可以繞過與創建新電影相關的GUI,因爲 這不是這些場景正在測試的。

*.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!(這最後一個只是純粹的絕望)......我做錯了什麼?

在先進的感謝:)

+1

我的錢是在你的正則表達式中錯過的冒號(':')。 –

+0

什麼正則表達式? '電影'是一個代表散列的變量 – cerealCode

+0

你有步驟「鑑於以下電影存在:」,然後嘗試將它與「給定/以下電影存在/電影_table |」匹配。你用來匹配步驟定義的正則表達式中的':'在哪裏? –

回答

0

看起來不錯。

您重複播放電影,然後在end之前做fail "Unimplemented"。你會期待什麼?

+0

你是第一次使用黃瓜,我看到'失敗',因爲代碼已經給出,我認爲我應該寫在塊中,所以我認爲'失敗'是因爲我最終會學習的原因 – cerealCode

+0

這個'失敗'是否有指示功能尚未準備好我猜 –