开发者

SQLite constraint exception on empty table when running rspec

开发者 https://www.devze.com 2023-04-12 02:33 出处:网络
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 wo

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号