0

I'm having a weird problem with my Rspec test suite. All tests that insert data into a table that has a unique constraint fail. Running the failing tests individually by specifying the line number works as expected.

To investigate the issue I printed the number of rows in that table before inserting the data that is causing the constraint exception and it reports that the table is empty.

There is no before :all in any file at all.

I'm using DatabaseCleaner and, except for this issue, it seems that the cleaning is working.

This e.g. doesn't work when running the whole file:

describe User

  # ...

  describe '#follow_location' do
    let(:user) { Factory(:user) }
    let(:location) { Factory(:location) }

    it 'should raise when trying to follow an already followed location' do
      puts LocationFollowship.count  # => 0
      user.followed_locations << location  # exception raised
      lambda {
        user.follow_location location
      }.should raise_error User::AlreadyFollowingLocation
    end
  end

  # …

end

EDIT: I was able to track it down. It has to do with Rails using a cached statement. Calling ActiveRecord::Base.connection.clear_cache! makes it work. But adding this snippet in spec_helper.rb causes an SQLite exception cannot use a closed statement.

2
  • can you show us your factories? :) maybe you added an id field Commented Oct 10, 2011 at 10:22
  • They don't set any explicit ids at all. Commented Oct 10, 2011 at 16:10

1 Answer 1

1

I had this problem too. In addition, the Ruby interpreter would segfault under certain conditions when I tried to investigate it (probably caused by SQLite).

I have a unique index declared, like so:

add_index(:table, [:column1, :column2], unique: true)

Adding the following uniqueness constraint to the model (in addition to the existing index in the migration) made the issue go away:

validates_uniqueness_of :column1, scope: :column2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.