簡體   English   中英

Ruby on Rails教程第8章測試錯誤

[英]ruby on rails tutorial chapter 8 test error

我在Michael Hartl的Ruby on Rails教程的8.2.1章中遇到了兩個不同的測試錯誤。 我是Rails的新手,但是我對語法錯誤和我能找到的其他任何東西進行了三重檢查。 很感謝任何形式的幫助。

錯誤消息1:ERROR [“ test_should_get_new”,Minitest :: Result,0.9693643249993329] test_should_get_new#Minitest :: Result(0.97s)NameError:NameError:未定義的局部變量或方法users_new_url' for #<UsersControllerTest:0x00000006e953f8> test/controllers/users_controller_test.rb:5:in在'

錯誤消息2:ERROR [“ test_invalid_signup_information”,Minitest :: Result,0.8977870759990765] test_invalid_signup_information#Minitest :: Result(0.90s)ActionController :: RoutingError:ActionController :: RoutingError:No route match [POST]“ / signup”測試/集成/users_signup_test.rb:8 block (2 levels) in <class:UsersSignupTest>' test/integration/users_signup_test.rb:7:in在'

的routes.rb

    Rails.application.routes.draw do

  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'
  get    '/login',   to: 'sessions#new'
  post   '/login',   to: 'sessions#create'
  delete '/logout',  to: 'sessions#destroy'
  resources :users

end

會話控制器:

    class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      # Log the user in and redirect to the user's show page.
    else
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
  end
end

用戶控制器:

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

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

  private

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

end

用戶控制器測試

    require 'test_helper'

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

end

用戶注冊測試

    require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest

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

  assert_select 'div#error_explanation'
  assert_select 'div.field_with_errors'
  assert_select 'form[action="/signup"]'
  end

  test "valid signup information" do
    get signup_path
    assert_difference 'User.count', 1 do
      post users_path, params: { user: { name: "Example User",
                                        email: "user@example.com", 
                                        password:             "password",
                                        password_confirmation: "password" } }
    end
    follow_redirect!
    assert_template 'users/show'
    assert_not flash.nil?
  end

end

在您的users_controller_test ,代碼不知道users_new_url是什么。 這可能是因為該路由不存在。 您很可能必須執行諸如get new_user_path ,但是您可以通過鍵入rake routes並獲取可用路由列表來查找。

這是rake routes將輸出的示例:

users     GET     /users(.:format)                    users#index
          POST    /users(.:format)                    users#create
new_user  GET     /users/new(.:format)                users#new
edit_user GET     /users/:id/edit(.:format)           users#edit
user      GET     /users/:id(.:format)                users#show

您可以通過在名稱后附加_path來引用命名路徑。 users_path將映射到"users#index"控制器和方法。

rake routes也將幫助您解決第二個問題,即您沒有為POST / signup定義的路由-您具有GET / signup。

因此,您需要添加如下一行:

post '/signup', to: 'users#create'

此路由將映射到UsersController#create方法,我在代碼中看到了該方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM