I am trying to create unit tests for my users model but when I add a
test that destroys a user, that user is not available in any of the
other test. If I remove the test that destroys the user all my other
tests work again.
All the fixtures are suppose be reloaded between tests right?
Here is my testcase :
require File.dirname(__FILE__) + ''/../test_helper''
class UserTest < Test::Unit::TestCase
fixtures :users
def setup
# get grant_speelman user
@first_user = User.find(1)
end
# Test updating of a user
def test_update
assert_equal @grant_speelman.firstname, @first_user.firstname
assert_equal @grant_speelman.surname, @first_user.surname
assert_equal @grant_speelman.email, @first_user.email
@first_user.firstname = "Gavin"
@first_user.surname = "Steenkamp"
@first_user.email = "test@glasshouse.co.za"
assert @first_user.save, @first_user.errors.full_messages.join(";
")
@first_user.reload
assert_equal "Gavin", @first_user.firstname
assert_equal "Steenkamp", @first_user.surname
assert_equal "test@glasshouse.co.za", @first_user.email
end
# Test deleting of user
def test_destroy
@first_user.destroy
assert_raise(ActiveRecord::RecordNotFound)
{User.find(@first_user.id)}
end
end
Here is the model :
require "digest/sha1"
class User < ActiveRecord::Base
has_many :tasks
has_many :assigned_tasks
attr_accessor :password
validates_presence_of :firstname, :surname
validates_presence_of :password, :on => :create
validates_confirmation_of :password, :on => :create
validates_format_of :email, :with => /^(.+)@(.+)\.[A-z]{2,6}$/
validates_uniqueness_of :email
def self.login(email, password)
hashed_password = hashed_password(password || "")
find(:first,
:conditions => ["email = ? and hashed_password = ?",
email,
hashed_password])
end
def try_to_login
User.login(self.email, self.password)
end
def before_create
self.hashed_password = User.hashed_password(self.password)
end
def after_create
@password = nil
end
private
def self.hashed_password(password)
Digest::SHA1.hexdigest(password)
end
end
Am I doing something wrong?
--
Posted via http://www.ruby-forum.com/.
Forgot to post the error message : 1) Error: test_update(UserTest): ActiveRecord::RecordNotFound: Couldn''t find User with ID=1 C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/base.rb:955:in `find_one'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/base.rb:941:in `find_from_ids'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/base.rb:382:in `find'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/fixtures.rb:401:in `find'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/fixtures.rb:227:in `instantiate_fixtures'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/fixtures.rb:225:in `each'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/fixtures.rb:225:in `instantiate_fixtures'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/fixtures.rb:224:in `silence'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/fixtures.rb:224:in `instantiate_fixtures'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/fixtures.rb:589:in `instantiate_fixtures'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/fixtures.rb:588:in `each'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/fixtures.rb:588:in `instantiate_fixtures'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/fixtures.rb:525:in `setup_with_fixtures'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/fixtures.rb:547:in `setup'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/fixtures.rb:546:in `setup'' 2 tests, 1 assertions, 0 failures, 1 errors -- Posted via http://www.ruby-forum.com/.
I find how to fix this problem : quote from another post : (Re: Order of tests matters???) --- quote The guide and the docs should really mention this problem. Transactional fixtures were turned on by default in Rails 1.0, to speed up testing. Unfortunately the default MySQL database type (MyISAM) doesn''t support transactional fixtures... The quick fix is to put the following line at the top of your test class (inside the class) self.use_transactional_fixtures = false The alternative fix is to change your table type to InnoDB (assuming you''re using MySQL) alter table products type=InnoDB; Then you won''t have to worry and your tests will be faster... --- end quote -- Posted via http://www.ruby-forum.com/.