测试rails app时出现奇怪的错误

时间:2014-07-03 14:17:20

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

我的测试非常简单:

    require 'test_helper'

class UserTest < ActiveSupport::TestCase

  test "should not save user without name" do
    user = User.new
    assert_not user.save
  end

end

我测试的用户如果没有名字就不应该保存到DB中。但是我收到了这个错误:

>rake test:units
    DL is deprecated, please use Fiddle
    Run options: --seed 36717

    # Running:

    E

    Finished in 0.159182s, 6.2821 runs/s, 0.0000 assertions/s.

      1) Error:
    UserTest#test_should_not_save_user_without_name:
    NoMethodError: undefined method `type' for nil:NilClass
        test/models/user_test.rb:7:in `block in <class:UserTest>'

1 runs, 0 assertions, 0 failures, 1 errors, 0 skips

UserDB:

    class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users, :id => false do |t|
      t.uuid :id, :primary_key => true
      t.string :user_name
      t.string :name, :null => false
      t.string :sur_name
      t.integer :phone
      t.string :email
      t.string :encrypted_password
      t.string :salt
      t.integer :age
      t.string :education
      t.timestamps
    end
  end
end

用户模型:

    require 'bcrypt'

class User < ActiveRecord::Base
    include ActiveUUID::UUID

    before_save :hash_new_password

    def hash_new_password
      @newPassword  = BCrypt::Password.create(self.encrypted_password)
      self.encrypted_password = @newPassword
    end
end

我做错了什么?我试图从rails网站下载本指南:rails test

0 个答案:

没有答案