Ruby on Rails教程 - 密码重置测试未通过(错误500)

时间:2014-12-18 18:21:44

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

我一直试图解决这个问题一段时间了。我一直得到同样的错误,我已经重新编写代码/尝试了不同的事情无济于事。

这是失败的测试:

FAIL["test_password_reset", PasswordResetsTest, 4.845783]
 test_password_reset#PasswordResetsTest (4.85s)
        Expected response to be a <redirect>, but was <500>
        test/integration/password_resets_test.rb:17:in `block in <class:PasswordResetsTest>'

这是测试本身:

require 'test_helper'

class PasswordResetsTest < ActionDispatch::IntegrationTest

  def setup
    ActionMailer::Base.deliveries.clear
    @user = users(:esteban)
  end

  test "password_reset" do
    get new_password_reset_path
    # Invalid submission
    post password_resets_path, password_reset: { email: "" }
    assert_template 'password_resets/new'
    # Valid submission
    post password_resets_path, password_reset: { email: @user.email }
    assert_redirected_to root_url
    # Get the user from the create action.
    user = assigns(:user)
    follow_redirect!
    assert_select 'div.alert'
    assert_equal 1, ActionMailer::Base.deliveries.size
    # Wrong email
    get edit_password_reset_path(user.reset_token, email: 'wrong')
    assert_redirected_to root_url
    # 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#email[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_nil flash.now
    assert_template 'password_resets/edit'
    # Valid password & confirmation
    patch_via_redirect password_reset_path(user.reset_token),
                       email: user.email,
                       user: { password:              "foobaz",
                               password_confirmation: "foobaz" }
    assert_template 'users/show'
  end
end

我理解500错误代码是什么,我只是不明白为什么我得到了一个,因为我已经遵循了代码,看起来一切都很好。我也要发布我的路线:

Rails.application.routes.draw do
  root                  'static_pages#home'
  get 'help'      =>    'static_pages#help'
  get 'about'     =>    'static_pages#about'
  get 'contact'   =>    'static_pages#contact'
  get 'signup'    =>    'users#new'
  get 'login'     =>    'sessions#new'
  post 'login'    =>    'sessions#create'
  delete 'logout' =>    'sessions#destroy'
  resources :users
  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]

任何帮助将不胜感激。谢谢。

编辑:现在,当我尝试注册该应用程序时,我收到以下错误:

undefined method `send_activation_email' for #<User:0x007faafbdb41f8>

以下是代码:

    @user = User.new(user_params)
      if @user.save
    @user.send_activation_email
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url
    else
     render 'new'
   end

1 个答案:

答案 0 :(得分:1)

在发生错误之后:

undefined method `send_activation_email' for #<User:0x007faafbdb41f8>

我发现代码中有一个小错误,特别是我在用户模型中拼错了send_activation_email。谢谢你的建议。