What best practices to setup enough data for integration test's scenario?

Do you usually have your scenarios go to exercise capybara steps in helper methods that we already extracted to create data or we call models/factory girl to create data? For example: In week 2, we need some todos data for scenarios “view my todo list” and “mark todos as complete”?

I think it depends on what you’re trying to test. When I’m trying to exercise the whole process as in a feature step, then I’ll use capybara to walk through each of the steps and create the data that way. If I just need the data to exercise some other portion of the app, then I’ll create data with FactoryGirl or in a helper method.

2 Likes

I do the same as @JESii

When you extract setting up data in a helper method, do you feel losing context of the data detail that you will use for expectation? For example, in a search scenario, we usually setup some data that will appear in the result, but we also we setup some data that won’t appear.

Another one more thing, when you need to setup a complex data in a scenario, do you like to keep in my your scenario or move it to before block? I like the idea to create a temp variable and give it a name with meaning of the data even I don’t reference it anywhere, so what do you think?

require 'spec_helper'

feature 'Admin exports Time to Completion report' do
  scenario 'export within date range' do

    not_yet_completed = create(:case, state: 'Pending-Initial Reabstraction')
    completed_without_pending = create(:case, :completed, completed_on: '2014-02-05')
    not_in_range = create(:case, :with_pending_started_at, completed_on: '2014-02-04')

    completed = create(:case, :with_pending_started_at, completed_on: '2014-02-05')

    login_as(:admin)
    export_report_on(start: '2014-02-05', finish: '2014-02-05')

    csv_has_one_row_with(completed)
  end

  def export_report_on(date_range)
    visit abstractors_path

    within('#time_completion_report') do
      fill_in 'Start', :with => date_range[:start]
      fill_in 'Finish', :with => date_range[:finish]
      click_button 'Export'
    end
  end

  def csv_has_one_row_with(c)
    expect(csv).to have(1).row
    expect(csv[0]['Case ID']).to eq(c.id.to_s)
  end
end