Authlogic和密码以及密码确认属性-无法访问? [英] Authlogic and password and password confirmation attributes - inaccessible?
问题描述
我正在尝试测试我登录(使用authlogic)后是否成功创建了一个新用户.我已经为用户添加了两个新字段,因此只想确保正确保存了用户.
Im trying to test my successfully creates a new user after login (using authlogic). Ive added a couple of new fields to the user so just want to make sure that the user is saved properly.
问题是尽管创建了有效的用户工厂,但每当我尝试抓住其属性以发布到create方法时,都会忽略密码和密码确认.我认为这是authlogic在后台执行的安全方法.这导致验证失败和测试失败.
The problem is despite creating a valid user factory, whenever i try to grab its attributes to post to the create method, password and password confirmation are being ommitted. I presuem this is a security method that authlogic performs in the background. This results in validations failing and the test failing.
我想知道如何解决这个问题?我可以手动输入属性,但看起来并不干燥.
Im wondering how do i get round this problem? I could just type the attributes out by hand but that doesnt seem very dry.
context "on POST to :create" do
context "on posting a valid user" do
setup do
@user = Factory.build(:user)
post :create, :user => @user.attributes
end
should "be valid" do
assert @user.valid?
end
should_redirect_to("users sentences index page") { sentences_path() }
should "add user to the db" do
assert User.find_by_username(@user.username)
end
end
##User factory
Factory.define :user do |f|
f.username {Factory.next(:username) }
f.email { Factory.next(:email)}
f.password_confirmation "password"
f.password "password"
f.native_language {|nl| nl.association(:language)}
f.second_language {|nl| nl.association(:language)}
end
推荐答案
您肯定无法从User对象读取密码和password_confirmation.您需要将:password
和:password_confirmation
合并到@user.attributes
哈希中.如果您将散列存储在其余工厂定义所共有的某个地方,则它不是超级干燥,但比将其硬编码到测试中更好.
You definitely can't read the password and password_confirmation from the User object. You will need to merge in the :password
and :password_confirmation
to the @user.attributes
hash. If you store that hash somewhere common with the rest of your factory definitions, it is not super dry, but it is better than hardcoding it into your test.
这篇关于Authlogic和密码以及密码确认属性-无法访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!