1

我试图测试我在登录后成功创建了一个新用户(使用 authlogic)。我已经向用户添加了几个新字段,因此只想确保正确保存用户。

问题是尽管创建了一个有效的用户工厂,但每当我尝试获取其属性以发布到 create 方法时,密码和密码确认都会被忽略。我认为这是 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
4

1 回答 1

2

您绝对无法从 User 对象中读取密码和密码确认。您需要将:passwordand合并:password_confirmation@user.attributes哈希中。如果您将该散列存储在与工厂定义的其余部分相同的地方,它并不是超级枯燥,但比将其硬编码到您的测试中要好。

于 2010-04-10T12:27:24.510 回答