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
.
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
精彩评论