为什么 Michael Hartl 在 Rails 书第 7 章的 Ruby 中的密码密钥的值为 "foo"?

Why the password key has a value of "foo" from Michael Hartl's Ruby on Rails book Chapter 7?

我目前正在按照 Michael Hartl 的 ROR 书创建 Web 应用程序。以下是来自 Chapter 7 的代码。

代码清单 7.21:test/integration/users_signup_test.rb

 require 'test_helper'

    class UsersSignupTest < ActionDispatch::IntegrationTest

      test "invalid signup information" do
        get signup_path
        assert_no_difference 'User.count' do
          post users_path, user: { name:  "",
                                   email: "user@invalid",
                                   password:              "foo",
                                   password_confirmation: "bar" }
        end
        assert_template 'users/new'
      end
    end

用于测试用户注册表单的有效性。我有点困惑为什么 nameemailpasswordpassword_confirmation都是这样填的吗? (意思是,例如,为什么 email 的值是 user@invalid;为什么 password 的 值是 foo?)

非常感谢,期待您的答复!

您正在测试无效的注册信息以确保未创建用户。您正在向用户创建操作执行 http post 请求,并且您正在发送该用户散列中的数据。 Foobar 是任意选择的值,主要是因为它们不匹配,因为需要密码和确认。

我同意 toddmetheny 的回答 事实上,您可以根据需要修改测试,例如

require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest

  test "invalid signup information" do
    get signup_path   # go to the sign up path 
    assert_no_difference 'User.count' do   # This assertion is checking that "username" should not be inserted in User table if the password and password_confirmation is not same   
      post users_path, user: { name:  "username",
                               email: "user@invalid",
                               password:              "123456",
                               password_confirmation: "123" }
    end
    assert_template 'users/new'  # This assertion is checking that after invalid password user should go to users/new template 
  end
end

希望我说得更清楚

根据 Chapter 7 from Ruby-on-Rails Tutorial,Micheal 试图在该部分解释如何在我们的注册页面上进行 验证 并向我们的申请明确说明要做什么接受用户和不接受用户的内容。

现在你的问题是为什么email填写为user@invalid。这是因为我们想要 测试我们在注册页面上应用的验证 。据此,电子邮件只有在格式类似于 user@invalid.com 时才有效;这使应用程序理解有效格式提交。

password 类似:foo,您可以看到我们有两个应该匹配的值,它们是 password 和 password_confirmation。在上面的代码中,它们 不匹配 所以正如我所解释的,它让应用程序明白这不是一个有效的提交。

最后,如代码所示,这些值在

test "invalid signup information" do

这让我们明白,如果信息如下,则显示消息无效注册信息,即:

name:  "",
email: "user@invalid",
password:              "foo",
password_confirmation: "bar"

所有这些都是不可接受的信息。