I am running a unit test on a user model in ruby. I am still learning ruby. So when I run
rake test:units
I get this
Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/rake_test_loader
Started
F
Finished in 0.116562 seconds.
1) Failure:
test_user_attributes_must_not_be_empty(UserTest) [test/unit/user_test.rb:10]:
<false> is not true.
1 tests, 5 assertions, 1 failures, 0 errors
rake aborted!
Command failed with status (1): [/usr/bin/ruby1.8 -I"lib:test" "/usr/lib/ru...]
Tasks: TOP => test:units
(See full trace by running task with --trace)
when I go to file test/unit/user_test.rb:10 I see this line,
require 'test_helper'
class UserTest < ActiveSupport::TestCase
test "user attributes must not be empty" do
user = User.new
assert user.invalid?
assert user.errors[:username].any?
assert user.errors[:name].any?
assert user.errors[:surname].any?
>>>assert user.errors[:date_birth].any?<<< Here!
assert user.errors[:date_reg].any?
assert user.errors[:email].any?
end
end
so what is it causing the error? Why am I getting the error there and not anywhere else? like username?
Edit:
My User Class:
class User < ActiveRecord::Base
validates :username, :name, :surname, :email, :presence => true
validates :username, :uniqueness => true
end
What your tests are saying is that if there is any error on all those attributes then the test pass.
You don’t have a validation on
date_birth, so no error is raised, so the test doesn’t pass.