未定义的局部变量或方法“login_path”

Undefined local variable or method `login_path'

我正在 rails 中为会话控制器做一些集成测试,在测试中我发现我的 login_path 没有定义,即使它在 routes.rb 中定义了文件,当我在控制台中调用该路径时,它也会显示出来。知道是什么原因造成的吗?

routes.rb:

Rails.application.routes.draw do
  get 'sessions/new'

  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
end

测试:

require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest
  get login_path
  assert_template 'sessions/new'
  post login_path, session: { email: "", password: "" }
  assert_template 'sessions/new'
  assert_not flash.empty?
  get root_path
  assert flash.empty?
end

如果我对 documentation 的理解正确,您应该调用 "test" 方法,然后将测试代码作为一个块传递。这样的东西应该可以工作

class UsersLoginTest < ActionDispatch::IntegrationTest
  test "user can log in" do
    get login_path
    assert_template 'sessions/new'
    post login_path, session: { email: "", password: "" }
    assert_template 'sessions/new'
    assert_not flash.empty?
    get root_path
    assert flash.empty?
  end
end