Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does Rails store data created by saving activerecord objects during tests?

Where does Rails store data created by saving activerecord objects during tests?

I thought I knew the answer to that question: obviously in the _test database. But it looks like this is not true!

I used this system to test what's happening to saved ActiveRecord data during rspec tests:

$ rails -d mysql test

$ cd test

$ nano config/database.yml ...

... create mysql databases test_test, test_development, test_production

$ script/generate rspec

$ script/generate rspec_model foo

edit Foo migration:

class CreateFoos 

$ rake db:migrate

edit spec/models/foo_spec.rb:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Foo do
  before(:each) do
    @valid_attributes = {
      :bar => 12345
    }
  end

  it "should create a new instance given valid attributes" do
    foo = Foo.new(@valid_attributes)
    foo.save
    puts "sleeping..."
    sleep(20)
  end
end

$ rake spec

When you see "sleeping...", change to another open terminal with a mysql session conneted to the test_test database and do:

mysql> select * from foos; Empty set (0.00 sec)

Why doesn't the mysql session show any records in the test_test database while the test is running?

like image 210
Rich Apodaca Avatar asked Jan 13 '09 02:01

Rich Apodaca


People also ask

What is ActiveRecord in Ruby on Rails?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.

What is the rails object relational mapping library?

Object Relational Mapping (ORM): simplify the use of databases in applications. Use objects to hold database records. Manage the movement of information between objects and the back-end database. Manage relationships between tables (joins), turn into linked data structures.

What is ActiveRecord base?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.


2 Answers

Items in the test database are erased by default after each test is run, by design. This is done to make sure that each of your tests has its own sandbox to play in that doesn't cause any interactions with the tests before it.

Again, this is by design. You don't want tests that are manipulating the same set of data (or rely on synchronous execution), because there is no guarantee of execution order.

However, I believe if you modify you test/test_helper.rb file to say this:

self.use_transactional_fixtures = false

instead of

self.use_transactional_fixtures = true

It will cause the data in your test database to persist.

ALSO: My advice is specifically designed to work with Test::Unit, not RSpec. However, I imagine that there is a similar setting your spec_helper.rb you should be looking for.

like image 149
Derek P. Avatar answered Sep 25 '22 02:09

Derek P.


You can observe records being added to the test database with:

tail -f log/test.log

You should see the transactions flying by as the tests are run.

like image 20
Martin Stannard Avatar answered Sep 23 '22 02:09

Martin Stannard