Authlogic 和密码以及密码确认属性 - 无法访问?
我正在尝试测试登录后是否成功创建新用户(使用 authlogic)。我已经向用户添加了几个新字段,因此只想确保正确保存用户。
问题是,尽管创建了一个有效的用户工厂,但每当我尝试获取其属性以发布到创建方法时,密码和密码确认都会被忽略。我认为这是 authlogic 在后台执行的安全方法。这会导致验证失败和测试失败。
我想知道如何解决这个问题?我可以手动输入属性,但这看起来不太干燥。
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
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您绝对无法从 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.