2 个测试在第 10.2.1 章结束时失败:没有路由匹配

2 tests fail at chapter 10.2.1 end: No route matches

我在尝试通过 Michael Hartl's Rails Tutorial book at the current state of the art. In particular, when I uncomment the before_action line in app/controllers/users_controller.rb according to listing 10.21 第 10.2.1 节末尾的测试时遇到问题,然后 运行 关于后续清单 10.22 的测试套件(其中测试应该是绿色的),两个测试失败了,即 test_should_redirect_edit_when_not_logged_intest_should_redirect_update_when_not_logged_in。这对我来说看起来很奇怪,因为我已经成功完成了所有步骤:\

无论如何,这是错误代码:

ERROR["test_should_redirect_edit_when_not_logged_in", UsersControllerTest, 1.0972138489887584]
 test_should_redirect_edit_when_not_logged_in#UsersControllerTest (1.10s)
ActionController::UrlGenerationError:         ActionController::UrlGenerationError: No route matches {:action=>"edit", :controller=>"users", :id=>nil} missing required keys: [:id]
            test/controllers/users_controller_test.rb:10:in `block in <class:UsersControllerTest>'

ERROR["test_should_redirect_update_when_not_logged_in", UsersControllerTest, 1.1339553739962867]
 test_should_redirect_update_when_not_logged_in#UsersControllerTest (1.13s)
ActionController::UrlGenerationError:         ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"users", :id=>nil} missing required keys: [:id]
            test/controllers/users_controller_test.rb:16:in `block in <class:UsersControllerTest>'

但是根据rake routes上面的路线已经存在了!

   Prefix Verb   URI Pattern               Controller#Action
edit_user GET    /users/:id/edit(.:format) users#edit
     user GET    /users/:id(.:format)      users#show

这是我的 app/controllers/users_controller.rb:

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:edit, :update]

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      log_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

  private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end

    # Before filters

    # Confirms a logged-in user.
    def logged_in_user
      unless logged_in?
        flash[:danger] = "Please log in."
        redirect_to login_url
      end
    end
end

我的test/controllers/users_controller_test.rb:

require 'test_helper'

class UsersControllerTest < ActionDispatch::IntegrationTest
  test "should get new" do
    get signup_path
    assert_response :success
  end

  test "should redirect edit when not logged in" do
    get edit_user_path(@user)
    assert_not flash.empty?
    assert_redirected_to login_url
  end

  test "should redirect update when not logged in" do
    patch user_path(@user), params: { user: { name: @user.name,
                                              email: @user.email } }
    assert_not flash.empty?
    assert_redirected_to login_url
  end
end

和我的 config/routes.rb:

Rails.application.routes.draw do

  get 'sessions/new'

  root 'static_pages#home'
  get  '/help',    to: 'static_pages#help'
  get  '/about',   to: 'static_pages#about'
  get  '/contact', to: 'static_pages#contact'
  get  '/signup',  to: 'users#new'
  post '/signup',  to: 'users#create'
  get    '/login',   to: 'sessions#new'
  post   '/login',   to: 'sessions#create'
  delete '/logout',  to: 'sessions#destroy'
  resources :users
end

望有胆大侠帮忙! :) 非常感谢

  test "should redirect edit when not logged in" do
    get edit_user_path(@user)

@user 为零。这就是导致错误的原因。我猜你跳过了一小段代码。