Authlogic 和密码以及密码确认属性 - 无法访问?

发布于 2024-08-28 08:00:45 字数 1101 浏览 7 评论 0原文

我正在尝试测试登录后是否成功创建新用户(使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

于我来说 2024-09-04 08:00:45

您绝对无法从 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文