Ruby-on-rails 使用 Rspec、Devise 和 Factory Girl 测试用户模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6032134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Testing the User Model with Rspec, Devise, and Factory Girl
提问by ardavis
I think there is a problem with my user factory being built. I'm getting an error saying that the password cannot be blank, but it's clearly set in my factories.rb. Does anyone see anything that I may be missing? Or a reason as to why the spec is failing? I do a very similar thing for one of my other models, and it seems to be successful. I'm not sure if the error is being caused by devise or not.
我认为我的用户工厂正在构建中存在问题。我收到一个错误,说密码不能为空,但它在我的 factory.rb 中明确设置。有没有人看到我可能遗漏的东西?或者规范失败的原因?我为我的其他模型之一做了一个非常相似的事情,它似乎很成功。我不确定错误是否是由设计引起的。
Rspec Error
规格错误
User should create a new instance of a user given valid attributes
Failure/Error: User.create!(@user.attributes)
ActiveRecord::RecordInvalid:
Validation failed: Password can't be blank
# ./spec/models/user_spec.rb:28:in `block (2 levels) in <top (required)>'
Factories.rb
工厂.rb
Factory.define :user do |user|
user.name "Test User"
user.email "[email protected]"
user.password "password"
user.password_confirmation "password"
end
user_spec.rb
用户规范.rb
require 'spec_helper'
describe User do
before(:each) do
@user = Factory.build(:user)
end
it "should create a new instance of a user given valid attributes" do
User.create!(@user.attributes)
end
end
user.rb
用户名
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
回答by apneadiving
In Factory Girl, this create attributes:
在 Factory Girl 中,这会创建属性:
@user_attr = Factory.attributes_for(:user)
And this create a new instance:
这将创建一个新实例:
@user = Factory(:user)
So change the above and try:
所以改变上面的并尝试:
User.create!(@user_attr)
In depth, what you're trying to do fails because:
深入地说,您尝试做的事情失败了,因为:
you were creating a new unsaved instance
password is a virtual attribute
the attributes of the instance do not contain the virtual attributes (I guess)
您正在创建一个新的未保存实例
密码是一个虚拟属性
实例的属性不包含虚拟属性(我猜)
回答by Isaac Betesh
The simplest approach IMO:
最简单的方法 IMO:
FactoryGirl.modify do
factory :user do
after(:build) { |u| u.password_confirmation = u.password = ... }
end
end
回答by kode
One tip that did the work for me. I was using FactoryGirl.create(:user)which didn't work. Changed it to this:
一个对我有用的技巧。我正在使用FactoryGirl.create(:user)它不起作用。改成这样:
user = FactoryGirl.build(:user)
user.password = "123456"
user.save
post :login, {:email => user.email, :password => "123456"}
# do other stuff with logged in user
This maybe is because 'password' is a virtual field. Hope this could hint someone.
这可能是因为“密码”是一个虚拟字段。希望这可以暗示某人。

