Hello
I’m wondering if someone can put me back on the right track?
I’ve started playing with Rails for the last 2 days while reading
through the Agile Web Dev with Rails book. I’m in Chapter 7 - Unit
Testing and Validation. The book demos work fine, but I’m now also
doing my own sample app to reinforce what I’m reading at the same
time.
I’m trying to do some Unit Tests on my User Model to check the length
of the password attribute.
Some of the assertions are failing unexpectedly and I’m now at a loss.
Can someone please help me find what I’m overlooking?
Here’s the Terminal output from running the Unit Tests:
turgs:snapshots tim$ rake test:units
Loaded suite /usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/
rake_test_loader
Started
…FF.
Finished in 0.531600 seconds.
-
Failure:
test_Password_must_be_long_enough(UserTest) [/users/tim/Sites/
snapshots/test/unit/user_test.rb:31]:
Failed assertion, no message given. -
Failure:
test_Password_must_be_shorter(UserTest) [/users/tim/Sites/snapshots/
test/unit/user_test.rb:47]:
Failed assertion, no message given.
7 tests, 25 assertions, 2 failures, 0 errors, 0 skips
Test run options: --seed 21159
rake aborted!
Command failed with status (1): [/usr/local/bin/ruby -I"lib:test" "/
usr/loc…]
Tasks: TOP => test:units
(See full trace by running task with --trace)
turgs:snapshots tim$
Here’s the User model code:
class User < ActiveRecord::Base
has_many :accounts, :dependent => :destroy
validates :email, :presence => true
validates :name, :presence => true
validates :password, :presence => true, :length =>
{ :minimum => 8, :maximum => 2000, :message => ‘should be between 8
and 2000 characters’ }
validates :security_question, :presence => true
validates :security_answer, :presence => true
validates :mobile_phone, :length =>
{ :minimum => 8, :maximum => 30, :message => ‘should be between 8 and
30 characters’}
end
Here’s the Unit Test user_test.rb file:
require ‘test_helper’
class UserTest < ActiveSupport::TestCase
test “User attributes must not be empty” do
user = User.new
assert user.invalid?
assert user.errors[:email].any?
assert user.errors[:name].any?
assert user.errors[:password].any?
assert user.errors[:security_question].any?
assert user.errors[:security_answer].any?
assert user.errors[:mobile_phone].any?
end
test “Password must be long enough” do
user = User.new(:email => “[email protected]”,
:name => “Adam”,
:security_question => “What’s my name?”,
:security_answer => “Adam”)
user.password = "1234567"
assert user.invalid?
assert_equal "should be between 8 and 2000 characters",
user.errors[:password].join(’; ')
user.password = "12345678"
assert user.valid? # this assertion is failing
user.password = "123456789"
assert user.valid?
end
test “Password must be shorter” do
user = User.new(:email => “[email protected]”,
:name => “Eve”,
:security_question => “What’s my name?”,
:security_answer => “Eve”)
user.password = "12345678901234567890...01234567890123456789" #
this is 1999 chars long, I’ve just cut it for this email.
assert user.valid? # this assertion is failing
user.password = "12345678901234567890...012345678901234567890" #
this is 2000 chars long, I’ve just cut it for this email.
assert user.valid?
user.password = "12345678901234567890...0123456789012345678901" #
this is 2001 chars long, I’ve just cut it for this email.
assert user.invalid?
assert_equal ‘should be between 8 and 2000 characters’,
user.errors[:password].join(’; ')
end
end
Any help would be greatly appreciated.
Cheers
Tim