从单元测试中调用模型方法

时间:2012-11-25 17:50:31

标签: ruby-on-rails unit-testing devise

我正在为我的模型添加单元测试,并且在使用gem 'devise'提供的用户模型测试时遇到问题。这是我的测试代码:

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  test "should not save user without name" do    
    user = User.new

    user.email = "david@test.com"
    user.password = "david123"

    assert !user.save, "Saved user without name"
  end
end

使用ruby -Itest test/unit/user_test.rb运行测试时出现以下错误:

  1) Error:
test_should_not_save_user_without_email(UserTest):
ActiveRecord::StatementInvalid: PG::Error: ERROR:  column "password" of relation "users"  does not exist
LINE 1: INSERT INTO "users" ("name", "email", "password", "created_a...
                                          ^
: INSERT INTO "users" ("name", "email", "password", "created_at", "updated_at", "id") VALUES ('David', 'david@test.com', 'david123', '2012-11-25 17:43:02', '2012-11-25 17:43:02', 127326141)

显然,没有调用devise提供的密码方法。让这个测试运行的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

刚才发现问题不是测试代码,而是我在用户的灯具中设置了password的值。