Authlogic and password and password confirmation attributes - inaccessible?

允我心安 提交于 2019-12-24 08:46:36

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/2612571/authlogic-and-password-and-password-confirmation-attributes-inaccessible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!