StandardNerd
StandardNerd

Reputation: 4183

How to run a scenario before another scenario in Rails Capybara test

What is the best way to run a scenario that depends on another scenario?

  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

this 'create a new category' must be done before another scenario 'edit category' can start:

  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

Is there a shortcut to eliminate the first 4 lines in 'edit category' scenario?

Upvotes: 1

Views: 779

Answers (1)

jfornoff
jfornoff

Reputation: 1368

Your tests shouldn't share state. That would make them dependent on each other, you don't want your "edit category" test to fail because your "create new category" functionality is broken.

The correct approach is to create a category object in the setup for the "edit category" test.

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

(This is FactoryGirl syntax, a very popular tool for that kind of data provisioning.

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

I'm not quite sure how you are using those fill-in-functions, but this is basically what you should be doing! ;-)

Cheers!

Upvotes: 1

Related Questions