无法在 Rspec 中创建用户模型

发布于 2024-12-18 07:02:22 字数 1169 浏览 0 评论 0原文

当我运行以下规范时:

require 'spec_helper'

describe User do
  before :each do
    @user = User.new :email => "[email protected]", :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 => "[email protected]", :password => "foobar", :password_confirmation => "foobar"
     ActiveRecord::UnknownAttributeError:
       unknown attribute: email
     # ./spec/models/user_spec.rb:5:in `new'

但是,当我进入控制台并运行时,

user = User.new :email => "[email protected]", :password => "foobar", :password_confirmation => "foobar"
user.valid?

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

When I run the following spec:

require 'spec_helper'

describe User do
  before :each do
    @user = User.new :email => "[email protected]", :password => "foobar", :password_confirmation => "foobar"
  end

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

I get this error:

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

However, when I go in to console and run

user = User.new :email => "[email protected]", :password => "foobar", :password_confirmation => "foobar"
user.valid?

It returns true. For some reason, in my test, I am unable to create a User instance, saying that the email attribute is inaccessible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天荒地未老 2024-12-25 07:02:22

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

Console uses the development database, but specs use the test database. Make sure email is defined in both.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文