Ruby“rake test:units”失败

时间:2011-12-06 12:28:12

标签: ruby-on-rails

我正在ruby中的用户模型上运行单元测试。我还在学习红宝石。所以当我跑

rake test:units

我明白了

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)

当我去归档test/unit/user_test.rb:10时,我看到了这一行,

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

那是什么导致错误?为什么我在那里得到错误而不是其他地方?像用户名?

编辑: 我的用户类:

class User < ActiveRecord::Base

validates :username, :name, :surname, :email, :presence => true
validates :username, :uniqueness => true

end

1 个答案:

答案 0 :(得分:2)

您的测试所说的是,如果所有这些属性都有错误,那么测试通过。

您没有对date_birth进行验证,因此不会引发任何错误,因此测试无法通过。