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 = "[email protected]"
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 "[email protected]", @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?