Test does not save to the test database

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=>“[email protected]”,:password=>“1234
56”)
=> #<User:0x53c9f9c @attributes={“updated_at”=>nil,
“screen_name”=>“dave”, “pass
word”=>“123456”, “created_at”=>nil, “email”=>“removed_ema[email protected]”},
@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 => “[email protected]”,
: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

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.

On Aug 28, 3:23 pm, Dave R. [email protected]
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

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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs