drthvdr
drthvdr

Reputation: 95

RSpec Tests fail with FactoryGirl, pass when isolated

I am upgrading an app from Rails 3 to Rails 4 and have a model called item with the following schema:

#===Schema Information
# Table name: items
#
#type :string
#maturity: string

and a spec with a series of test, but this one in particular is the one I have the issue with:

describe 'next_version' do
it 'should return the created_at date of the Item one version higher' do
first = FactoryGirl.create(:item, maturity: 'PRODUCTION')
second = FactoryGirl.create(:item, maturity: 'PREPRODUCTION', created_at: Time.now.in_time_zone)

expect(first.next_version_created_at.to_s)to eq second.created_at.utc.to_s)

The fix, I believe is easy enough.. just add..

 created_at: Time.now.in_time_zone

..to the Production item and done. However, the test passes if I run it by itself, and the entire spec passed (146 tests) in Rails 3, however when I run the entire spec now this one and 3 other similar cases fail, unless I run them alone.

Thoughts?

Upvotes: 2

Views: 360

Answers (2)

user3896760
user3896760

Reputation: 69

You could try reloading factory girl before each test. Try adding this to the spec_helper.rb

config.before(:each) do
  FactoryGirl.reload
end

Upvotes: 2

tirdadc
tirdadc

Reputation: 4713

Usually this happens when you have data that isn't being properly cleaned up in the test database.

Make sure you have:

config.use_transactional_fixtures = false

in spec_helper.rb or rails_helper.rb.

See here:

rspec test passes in isolation, but fails when run with other tests

RSpec errors when run en masse, but not individually

Upvotes: 1

Related Questions