无法在Rspec中创建用户模型

时间:2011-11-27 18:37:34

标签: ruby-on-rails rspec devise

当我运行以下规范时:

require 'spec_helper'

describe User do
  before :each do
    @user = User.new :email => "foo@bar.com", :password => "foobar", :password_confirmation => "foobar"
  end

  it "should be valid" do
    @user.should be_valid
  end
end

我收到此错误:

  1) User should be valid
     Failure/Error: @user = User.new :email => "foo@bar.com", :password => "foobar", :password_confirmation => "foobar"
     ActiveRecord::UnknownAttributeError:
       unknown attribute: email
     # ./spec/models/user_spec.rb:5:in `new'

然而,当我进入控制台并运行

user = User.new :email => "foo@bar.com", :password => "foobar", :password_confirmation => "foobar"
user.valid?

它返回true。出于某种原因,在我的测试中,我无法创建用户实例,说电子邮件属性无法访问。

1 个答案:

答案 0 :(得分:3)

控制台使用开发数据库,​​但规范使用测试数据库。确保两者都定义了email