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”=>“[email protected]”},
@new_record=tru
e>user.save <<<<THIS DOES ACTUALLY SAVE THE INFO TO THE TEST DATABASE!!!
=> trueexit
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