# Records created while tests are running are not being saved

**URL:** <https://www.ruby-forum.com/t/records-created-while-tests-are-running-are-not-being-saved/185327>\
**Category:** Rails\
**Created:** [March 18, 2010, 11:09am UTC](https://www.ruby-forum.com/t/records-created-while-tests-are-running-are-not-being-saved/185327 "2010-03-18T11:09:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikrant\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e5b9ba/32.png) [@Vikrant\_C](https://www.ruby-forum.com/u/Vikrant_C)\
**Post date:** [March 18, 2010, 11:09am UTC](https://www.ruby-forum.com/t/records-created-while-tests-are-running-are-not-being-saved/185327/1 "2010-03-18T11:09:22Z")

</div>

In following code -

require “test\_helper”  
Class SomeControllerTest \< ActionController::TestCase

def test\_something  
record = SomeModel.create :name =\> ‘test’  
record.save!  
gets  
end

end

Above record is not being saved in the database. I can confirm this by  
pausing execution by calling gets() and doing a “SELECT \* FROM  
some\_models” right into the test database which returns no rows.

---

<div class="post-metadata">

**Author:** ![Vikrant\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e5b9ba/32.png) [@Vikrant\_C](https://www.ruby-forum.com/u/Vikrant_C)\
**Post date:** [March 18, 2010, 11:15am UTC](https://www.ruby-forum.com/t/records-created-while-tests-are-running-are-not-being-saved/185327/2 "2010-03-18T11:15:13Z")

</div>

On Thu, Mar 18, 2010 at 6:08 AM, Vikrant C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end
> 
> Above record is not being saved in the database. I can confirm this by  
> pausing execution by calling gets() and doing a “SELECT \* FROM  
> some\_models” right into the test database which returns no rows.
> 
> –

Tests are ran inside a transaction and are rolled back after each  
test. In a separate process (i.e. ./script/dbconsole) you will not see  
the data create from a test, even if you pause execution.

You _should_ be able to see the insert (and the roll back) happening in  
test.log

Even though the transaction is rolled back, the sequence on the  
primary key is still advanced, so you should see that effect in a  
separate process.

- Steven

---

<div class="post-metadata">

**Author:** ![Vikrant\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e5b9ba/32.png) [@Vikrant\_C](https://www.ruby-forum.com/u/Vikrant_C)\
**Post date:** [March 18, 2010, 4:23pm UTC](https://www.ruby-forum.com/t/records-created-while-tests-are-running-are-not-being-saved/185327/3 "2010-03-18T16:23:46Z")

</div>

Thanks.

---

<div class="post-metadata">

**Author:** ![Vikrant\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e5b9ba/32.png) [@Vikrant\_C](https://www.ruby-forum.com/u/Vikrant_C)\
**Post date:** [March 19, 2010, 5:23am UTC](https://www.ruby-forum.com/t/records-created-while-tests-are-running-are-not-being-saved/185327/4 "2010-03-19T05:23:31Z")

</div>

Just for records. Line #47 -  
[http://code.google.com/p/kopal/source/browse/test/network/connect\_controller\_test.rb?r=876e7c80c420e5faca9492bd50ed1dd943cb90ac](http://code.google.com/p/kopal/source/browse/test/network/connect_controller_test.rb?r=876e7c80c420e5faca9492bd50ed1dd943cb90ac)

---

<div class="post-metadata">

**Author:** ![Vikrant\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e5b9ba/32.png) [@Vikrant\_C](https://www.ruby-forum.com/u/Vikrant_C)\
**Post date:** [March 18, 2010, 7:01pm UTC](https://www.ruby-forum.com/t/records-created-while-tests-are-running-are-not-being-saved/185327/5 "2010-03-18T19:01:16Z")

</div>

On Mar 18, 10:13 am, Steven H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Thu, Mar 18, 2010 at 6:08 AM, Vikrant C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> Tests are ran inside a transaction and are rolled back after each  
> test. In a separate process (i.e. ./script/dbconsole) you will not see  
> the data create from a test, even if you pause execution.

you can see the data during the test if you alter the transaction  
isolation level to read uncommitted (at least you can with mysql)

Fred
