Why can't my Rails tests see the contents of my SQLite3 database in memory?

I am trying to speed up tests for a Rails application using an in-memory SQLite3 database as a test database for the application. I followed the instructions in this post.

My database configuration in database.yml as follows:

 test: adapter: sqlite3 database: ":memory:" encoding: utf8 verbosity: quiet 

I also created an initializer, as recommended:

 def in_memory_database? Rails.env == "test" and ActiveRecord::Base.connection.class == ActiveRecord::ConnectionAdapters::SQLiteAdapter || ActiveRecord::Base.connection.class == ActiveRecord::ConnectionAdapters::SQLite3Adapter and Rails.configuration.database_configuration['test']['database'] == ':memory:' end if in_memory_database? ActiveRecord::Schema.verbose = false puts "creating sqlite in memory database" load "#{Rails.root}/db/schema.rb" end 

All my tests fail because the devices cannot be loaded, as you can see:

 creating sqlite in memory database Loaded suite test/unit/vendor_appliance_test Started E Finished in 0.070079 seconds. 1) Error: test_the_truth(VendorApplianceTest): ActiveRecord::StatementInvalid: Could not find table 'advertisers' 1 tests, 0 assertions, 0 failures, 1 errors 

The initializer loads the database (as you can see from the first line of output), but for some reason the circuit is not visible in the tests because it does not look in the right place or because it was wiped by the time the tests started.

Has anyone seen this before?

+4
source share
2 answers

Take a look at about common connections:

http://blog.plataformatec.com.br/2011/12/three-tips-to-improve-the-performance-of-your-test-suite/

sqllite supports db in memory in every connection. the active record uses the connection pool and uses a different connection when starting the test. You can check the identifiers of connection objects at different points to see the actual changes to the objects.

if you use one connection, you should get the expected behavior.

and load faster :-)

+2
source

Just enter the code below in test_helper.rb

 def in_memory_database? ActiveRecord::Base.connection.class == ActiveRecord::ConnectionAdapters::SQLiteAdapter || ActiveRecord::Base.connection.class == ActiveRecord::ConnectionAdapters::SQLite3Adapter and Rails.configuration.database_configuration['test']['database'] == ':memory:' end class ActiveSupport::TestCase if in_memory_database? ActiveRecord::Schema.verbose = false puts "creating sqlite in memory database" load "#{Rails.root}/db/schema.rb" end end 
+1
source

Source: https://habr.com/ru/post/1403724/


All Articles