# Test does not save to the test database

**URL:** <https://www.ruby-forum.com/t/test-does-not-save-to-the-test-database/113216>\
**Category:** Rails\
**Created:** [August 28, 2007, 10:23pm UTC](https://www.ruby-forum.com/t/test-does-not-save-to-the-test-database/113216 "2007-08-28T22:23:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [August 28, 2007, 10:23pm UTC](https://www.ruby-forum.com/t/test-does-not-save-to-the-test-database/113216/1 "2007-08-28T22:23:43Z")

</div>

even though i change the RAIL\_ENV…to test and do both a rake  
test:functionals and run an individual  
ruby test\functional\user\_controller\_test.rb test and run script/console  
that confirms  
‘Loading test environment’…all of my tests (but the script/console)  
below do not save anything in  
to the test database … i’m running on a W2000 with rails 1.2.3 and  
ruby 1.8.6.4…please help this nuby…thanx dave  
database.yml  
development:  
adapter: oracle  
database: raild  
username: scott  
password: tiger  
host: localhost

# Warning: The database defined as ‘test’ will be erased and

# re-generated from your development database when you run ‘rake’.

# Do not set this db to the same as development or production.

test:  
adapter: oracle  
database: railt  
username: scott  
password: tiger  
host: localhost

production:  
adapter: oracle  
database: railp  
username: scott  
password: tiger  
host: localhost

C:\railsspace\>set RAILS\_ENV  
RAILS\_ENV=test \<\<SEE THIS SHOULD SAVE EVERYTHING TO THE TEST DATABASE…

C:\railsspace\>ruby test\functional\user\_controller\_test.rb  
Loaded suite test/functional/user\_controller\_test  
Started  
…  
Finished in 0.812 seconds.

4 tests, 16 assertions, 0 failures, 0 errors

C:\railsspace\>set RAILS\_ENV  
RAILS\_ENV=test

C:\railsspace\>script/console

C:\railsspace\>ruby script/console  
Loading test environment.

> > user=User.new(:screen\_name=\>“dave”,:email=\>“removed\_email\_address@domain.invalid”,:password=\>“1234  
> > 56”)  
> > =\> #\<User:0x53c9f9c @attributes={“updated\_at”=\>nil,  
> > “screen\_name”=\>“dave”, “pass  
> > word”=\>“123456”, “created\_at”=\>nil, “email”=\>“removed\_email\_address@domain.invalid”},  
> > @new\_record=tru  
> > e\>
> 
> > user.save \<\<\<\<THIS DOES ACTUALLY SAVE THE INFO TO THE TEST DATABASE!!!  
> > =\> true
> 
> > exit

C:\railsspace\>type rake.bat  
c:\ruby186\ruby\bin\ruby c:\ruby186\ruby\bin\rake %1 %2 %3  
C:\railsspace\>exit

user\_controller\_test.rb contains:

require File.dirname( **FILE** ) + ‘/…/test\_helper’  
require ‘user\_controller’

# Re-raise errors caught by the controller.

class UserController; def rescue\_action(e) raise e end; end

class UserControllerTest \< Test::Unit::TestCase  
def setup  
@controller = UserController.new  
@request = ActionController::TestRequest.new  
@response = ActionController::TestResponse.new  
end

# Replace this with your real tests.

def test\_truth  
assert true  
end

```
# Make sure the registration page responds with the proper form.

```

def test\_registration\_page  
get :register  
title = assigns(:title)  
assert\_equal “Register”, title  
assert\_response :success  
assert\_template “register”  
# Test the form and all its tags.  
assert\_tag “form”, :attributes =\> { :action =\> “/user/register”,  
:method =\> “post” }  
assert\_tag “input”,  
:attributes =\> { :name =\> “user[screen\_name]”,  
:type =\> “text”,  
:size =\> User::SCREEN\_NAME\_SIZE,  
:maxlength =\>  
User::SCREEN\_NAME\_MAX\_LENGTH }  
assert\_tag “input”,  
:attributes =\> { :name =\> “user[email]”,  
:type =\> “text”,  
:size =\> User::EMAIL\_SIZE,  
:maxlength =\> User::EMAIL\_MAX\_LENGTH }  
assert\_tag “input”,  
:attributes =\> { :name =\> “user[password]”,  
:type =\> “password”,  
:size =\> User::PASSWORD\_SIZE,  
:maxlength =\> User::PASSWORD\_MAX\_LENGTH  
}  
assert\_tag “input”, :attributes =\> { :type =\> “submit”,  
:value =\> “Register!” }  
end

```
# Test a valid registration.

```

def test\_registration\_success  
post :register, :user =\> { :screen\_name =\> “new\_screen\_name”,  
:email =\> “removed\_email\_address@domain.invalid”,  
:password =\> “long\_enough\_password” }  
# Test assignment of user.  
user = assigns(:user)  
assert\_not\_nil user  
# Test new user in database.  
new\_user = User.find\_by\_screen\_name\_and\_password(user.screen\_name,  
user.password)  
assert\_equal new\_user, user  
# Test flash and redirect.  
assert\_equal “User #{new\_user.screen\_name} created!”, flash[:notice]  
assert\_redirected\_to :action =\> “index”  
end

```
def test_index
get :index
title = assigns(:title)
assert_equal "RailsSpace User Hub", title
assert_response :success
assert_template "index"
end

```

end

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [September 4, 2007, 1:07am UTC](https://www.ruby-forum.com/t/test-does-not-save-to-the-test-database/113216/2 "2007-09-04T01:07:28Z")

</div>

I was just going to write with the same issue. I know one approach to  
testing is to wrap the whole test in a transaction that gets rolled  
back at the end of the test, so I’m wondering if the test harnesses  
use that approach under the covers.

Anyway, I am trying to get the SaltedLogin generator tests to work and  
am stuck on test\_change\_password in the bad\_email flow - it seems  
like, when the email fails and an error is raised, the idea is that  
the update should be rolled back, but stepping through the code, it  
appears that transaction\_open is false, even though the  
change\_password function is wrapped in a User.transaction() block.  
Not being able to view the state of the database is making this hard  
to confirm.

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [September 4, 2007, 4:39am UTC](https://www.ruby-forum.com/t/test-does-not-save-to-the-test-database/113216/3 "2007-09-04T04:39:44Z")

</div>

On Aug 28, 3:23 pm, Dave R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> # Warning: The database defined as ‘test’ will be erased and
> 
> # re-generated from your development database when you run ‘rake’.
> 
> # Do not set this db to the same as development or production.

Read this carefully. The test database is blown out every time tests  
are run. You should be using fixtures or mocks.

Rein

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [September 4, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/test-does-not-save-to-the-test-database/113216/4 "2007-09-04T16:29:38Z")

</div>

The database is blown out BEFORE the tests are run, not during or  
after, near as I can tell; I personally am using fixtures, which end  
up causing records to be inserted in the db, and they remain there  
after the tests are run, unchanged by the tests.

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [September 4, 2007, 5:56pm UTC](https://www.ruby-forum.com/t/test-does-not-save-to-the-test-database/113216/5 "2007-09-04T17:56:00Z")

</div>

sydneyos wrote:

> The database is blown out BEFORE the tests are run, not during or  
> after, near as I can tell; I personally am using fixtures, which end  
> up causing records to be inserted in the db, and they remain there  
> after the tests are run, unchanged by the tests.

The tests run inside a big transaction.  
How can you write a test, unless you know what the database is going to  
look like at the time it gets run?

An example is this;

You have two tests.

def test\_delete\_everything

# we’ve put fixtures in place, so there is something to delete

assert\_operator 0, :\<, Model.count

Model.delete\_everything  
assert\_equal 0, Model.count  
end

def test\_make\_a\_whole\_load\_of\_stuff

# we know there are 5 models in the fixtures

# so lets just assert that there is something in here before hand

count\_before = Model.count  
assert\_operator 0, :\<, count\_before

# a whole load of stuff is actually just 3 new models

Model.make\_a\_whole\_load\_of\_stuff  
assert\_equal count\_before + 3, Model.count  
end

If the delete test gets run first, our database will be empty, and break  
the 2nd test,  
if it gets run second then it’ll pass.

So, instead, the transaction ensures that the database starts and ends  
in the same state. And that state is expected to be just the fixtures  
you define for it.
