2014-09-26 126 views
1

運行依賴於另一場景的場景的最佳方式是什麼?如何在Rails Capybara測試的另一個場景之前運行場景

scenario 'create a new category' do 
    index_page.open 
    index_page.click_new_category 
    modify_page.fill_form_with(category_params) 
    modify_page.submit 
    expect(index_page.flash).to have_css('.alert-success') 
    expect(index_page.entry(1)).to have_content(category_params[:name_de]) 
    end 

這個「創建一個新的類別」之前必須另一個場景「編輯類別」做就可以開始:

scenario 'edit category' do 
    index_page.open 
    index_page.click_new_category 
    modify_page.fill_form_with(category_params) 
    modify_page.submit 
    index_page.open 
    index_page.click_edit_category 
    modify_page.fill_form_with(category_params) 
    modify_page.submit 
    expect(index_page).to have_css('.alert-success') 
    end 

是否有一個快捷方式,以消除前4行中的「編輯類」方案?

回答

1

您的測試不應共享狀態。這會讓他們相互依賴,你不希望你的「編輯類別」測試失敗,因爲你的「創建新類別」功能被破壞了。

正確的做法是在「編輯類別」測試的設置中創建一個類別對象。

let(:category) { create(:category) } 

(這是FactoryGirl語法,一個非常流行的那種數據配置的工具。

scenario 'edit category' do 
    visit edit_category_path(category) 
    page.fill_form_with(category_params) 
    page.submit 
    expect(page).to have_css('.alert-success') 
end 

我不太清楚你是如何使用這些填充式的功能,但是這基本上你應該做什麼!;-)

乾杯!