无法在 Rspec 中创建用户模型
当我运行以下规范时:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Console 使用开发数据库,但规范使用测试数据库。确保两者中都定义了
email
。Console uses the development database, but specs use the test database. Make sure
email
is defined in both.