NameError:未初始化的常量UsersControllerTest :: FILL_IN

时间:2016-04-21 18:16:38

标签: ruby-on-rails

我目前正在编写Michael Hartls RoR教程的第10章第1部分,在10.25的测试中,我的测试总是红色。我已经浏览了所有材料和信息,并在网上看到了 - 我仍然没有解决方案......

第一条错误消息:

ERROR["test_should_not_allow_the_admin_attribute_to_be_edited_via_the_web", UsersControllerTest, 1.8564385510981083]
 test_should_not_allow_the_admin_attribute_to_be_edited_via_the_web#UsersControllerTest (1.86s)
NameError:         NameError: uninitialized constant UsersControllerTest::FILL_IN
            test/controllers/users_controller_test.rb:64:in `block in <class:UsersControllerTest>'

这是测试:

test "should not allow the admin attribute to be edited via the web" do
    log_in_as(@other_user)
    assert_not @other_user.admin?
    patch :update, id: @other_user, user: { password:              FILL_IN,
                                            password_confirmation: FILL_IN,
                                            admin: FILL_IN }
    assert_not @other_user.FILL_IN.admin?
  end

我不知道FILL_IN来自哪里,因为我在两个文件之间有一定的周数......

第二个是:

FAIL["test_valid_signup_information", UsersLoginTest, 2.0111586400307715]
 test_valid_signup_information#UsersLoginTest (2.01s)
        expecting <"users/show"> but rendering with <["user_mailer/account_activation", "layouts/mailer", "static_pages/home", "layouts/_shim", "layouts/_header", "layouts/_footer", "layouts/application"]>
        test/integration/users_login_test.rb:48:in `block in <class:UsersLoginTest>'

测试看起来像这样:

test "login with valid information followed by logout" do
    get login_path
    post login_path, session: { email: @user.email, password: 'password' }
    assert is_logged_in?
    assert_redirected_to @user
    follow_redirect!
    assert_template 'users/show'
    assert_select "a[href=?]", login_path, count: 0
    assert_select "a[href=?]", logout_path
    assert_select "a[href=?]", user_path(@user)
    delete logout_path
    assert_not is_logged_in?
    assert_redirected_to root_url
    # Simulate a user clicking logout in a second window.
    delete logout_path
    follow_redirect!
    assert_select "a[href=?]", login_path
    assert_select "a[href=?]", logout_path,      count: 0
    assert_select "a[href=?]", user_path(@user), count: 0
  end

  test "valid signup information" do
    get signup_path
    assert_difference 'User.count', 1 do
      post_via_redirect users_path, user: { name:  "Example User",
                                            email: "user@example.com",
                                            password:              "password",
                                            password_confirmation: "password" }
    end
    assert_template 'users/show'
    assert is_logged_in?
  end

请告诉我还有什么可以作为解决这个难题的输入。

啊,尝试登录第二个测试的用户是:

  def setup
    @user = users(:michael)
  end

否则称为:

michael:
  name: Michael Example
  email: michael@example.com
  password_digest: <%= User.digest('password') %>
  admin: true
  activated: true
  activated_at: <%= Time.zone.now %>

我感谢任何提示......谢谢你们!

1 个答案:

答案 0 :(得分:2)

在您的测试文件中,显示FILL_IN,您必须填写自己的数据。

test "should not allow the admin attribute to be edited via the web" do
    log_in_as(@other_user)
    assert_not @other_user.admin?

    #  =======  HERE =======
    patch :update, id: @other_user, user: { password:              FILL_IN,
                                            password_confirmation: FILL_IN,
                                            admin: FILL_IN }

    #  =======  HERE AS WELL =======
    assert_not @other_user.FILL_IN.admin?
  end

我想你需要像这样填写那些占位符:

test "should not allow the admin attribute to be edited via the web" do
    log_in_as(@other_user)
    assert_not @other_user.admin?

    #  =======  HERE =======
    patch :update, id: @other_user, user: { password:              "user's password",
                                            password_confirmation: "user's password",
                                            admin: true }

    #  =======  HERE AS WELL =======
    assert_not @other_user.reload.admin?
  end