2011-11-13 63 views
2

我有一種情況是這樣的測試/種子數據:黃瓜表定義和

Scenario: Display some articles 
    Given the following article pages: 
    | title | body  | 
    | test | hello world | 

我見過一些人創造xxx_steps.rb,然後動態生成的數據。

當黃瓜運行時,默認運行RAILS_ENV = test嗎?它會加載seeds.rb每次運行一個新的測試數據庫?如果沒有,我該如何做到這一點?

此外,黃瓜是否加載/ step_definitions中的所有文件,或者是否存在某種約定,它加載了與當前特徵文件相匹配的任何文件?

回答

1

要在飛行中生成數據,我會建議您使用factory_girl gem。 您可以在控制其他人時自動生成字段。

在這種情況下,我有兩個數組的名稱(first_names和last_names),我會隨機創建Contact對象。

FactoryGirl.define do 

    factory :contact do 
    name {"#{first_names.sample} #{last_names.sample}"} 
    email {"#{name.downcase.gsub!(' ', '.')}@example.com"} 
    vat_number {rand(9999999).to_s.center(7, rand(9).to_s)} 
    gender {['male', 'female'].sample} 
    birth_date {Date.today - rand(200..40000)} 
    end 
end 

然後創建一個聯繫我只需要FactoryGirl.create(:contact)爲單個記錄。 您可以調整此示例以適應您的需求。

在你的情況,你可以用你的表是這樣的:

Given /^the following article pages:$/ do |table| 
    table.hashes.each do |hash| 
    FactoryGirl.create(:article, hash 
end