【问题标题】:Reading MiniTest failing messages阅读 MiniTest 失败消息
【发布时间】:2014-12-10 07:31:22
【问题描述】:

我的测试失败,我似乎无法正确阅读它。你会如何阅读这篇文章:

FAIL["test_password_resets", PasswordResetsTest, 0.310017]
   test_password_resets#PasswordResetsTest (0.31s)
          expecting <"password_resets/new"> but rendering with <[]>
          test/integration/password_resets_test.rb:15:in `block in <class:PasswordResetsTest>'

    1/1: [===================================] 100% Time: 00:00:00, Time: 00:00:00

  Finished in 0.31430s
  1 tests, 1 assertions, 1 failures, 0 errors, 0 skips

我该去哪里修复错误?它真的给我的不仅仅是测试表上的行号。但这并不能真正帮助我找到最终的解决方案。

这是测试:

    test "password resets" do
    get new_password_reset_path
    assert_template 'password_resets/new'
    # invalid email
    post password_resets_path, password_reset: { email: "" }
    assert_not flash.empty?
    assert_template 'password_resets/new'
    # valid email
    post password_resets_path, password_reset: { email: @user.email }
    assert_not_equal @user.reset_digest, @user.reload.reset_digest
    assert_equal 1, ActionMailer::Base.deliveries.size
    assert_not flash.empty?
    assert_redirected_to root_url
    # password reset form
    user = assigns(:user)
    # wrong email
    get edit_password_reset_path(user.reset_token, email: "")
    assert_redirected_to root_url
    # inactive user
    user.toggle!(:activated)
    get edit_password_reset_path(user.reset_token, email: user.email)
    assert_redirected_to root_url
    user.toggle!(:activated)
    # right email, wrong token
    get edit_password_reset_path('wrong token', email: user.email)
    assert_redirected_to root_url
    # right email, right token
    get edit_password_reset_path(user.reset_token, email: user.email)
    assert_template 'password_resets/edit'
    assert_select "input[name=email][type=hidden][value=?]", user.email
    # invalid password & confirmation
    patch password_reset_path(user.reset_token),
          email: user.email,
          user: { password:              "foobaz",
                  password_confirmation: "barquux" }
    assert_select 'div#error_explanation'
    # blank password & confirmation
    patch password_reset_path(user.reset_token),
          email: user.email,
          user: { password:              "  ",
                  password_confirmation: "  " }
    assert_not flash.empty?
    assert_template 'password_resets/edit'
    # valid password & confirmation
    patch password_reset_path(user.reset_token),
          email: user.email,
          user: { password:              "foobaz",
                  password_confirmation: "foobaz" }
    assert is_logged_in?
    assert_not flash.empty?
    assert_redirected_to user
  end

第 15 行是“assert_template 'password_resets/new'”第一行,严格来说是第 3 行。

提前谢谢你。

【问题讨论】:

  • (1) 确保new_password_reset_path 是有效的命名路由,但正在运行rake routes (2) 确保您的测试使用config.include Rails.application.routes.url_helpers 识别它
  • 你的控制器测试告诉你这个路由/请求什么?它是否使用您期望的响应代码呈现您期望的模板?
  • 我要补充的是,这是一组异常复杂的交互,即使对于集成测试也是如此。 10 个控制器请求很多,我猜您可以轻松地将其分解为 2 或 3 个单独的测试,以使其更具可读性和可管理性。
  • 问题是我的路由文件中的 new_password_reset_path 拼写错误。我最终确实将它们分成 3 个不同的测试以使其更具可读性。

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 minitest


【解决方案1】:

不要断言模板已呈现,而是尝试断言响应是您所期望的,然后然后断言模板是正确的。

assert_response :success
assert_template 'password_resets/new'

我怀疑请求被重定向,它不会呈现模板。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多