2014-09-26 79 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 

我不太清楚你是如何使用这些填充式的功能,但是这基本上你应该做什么!;-)

干杯!