Question

I am writing a test using Capybara with Rspec and I reached the step in which I must select a dropdown option. The dropdown list contains categories (for some activities), which are retrieved from the database, in the development environment. When testing they are no longer present as options in the dropdown.

How can I populate the dropdown in testing environment?

I have the following factories:

FactoryGirl.define do
  factory :category do
    name { 'art' }
  end
end

FactoryGirl.define do
  factory :activity do
   title       { Faker::Lorem.sentence[0...Activity::MAX_TITLE_LENGTH] }
   description { Faker::Lorem.sentence }

   factory :full_activity do
     categories  { |pa| [ pa.association(:category) ] }
     venues      { [ FactoryGirl.build(:venue, :city => location) ] }
   end   
  end
end

And in my feature test there is:

require 'spec_helper'

feature "add new activity", js: true do

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

  let(:activity) do
    FactoryGirl.create({
        venues: [FactoryGirl.create(:venue)]
    })
  end

  scenario "user fills step 1" do

      visit root_path

      click_on("Add activity")

      expect(page).to have_content("Categories")

      page.select 'art', :from => 'category_dd_id'

      page.find("#save_step_1").click
  end
end

In /models/activity.rb:

has_and_belongs_to_many :categories

In /models/category.rb:

has_and_belongs_to_many :activities
Was it helpful?

Solution

I have managed to make the test work well by adding let!, instead of let. The answer to this question helped me.
More info on let and let! here.

let!(:category) { FactoryGirl.create(:category)}

let!(:activity) do
    FactoryGirl.create({
      venues: [FactoryGirl.create(:venue)]
  })
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top