我不断出现错误:我的路由文件已经有login_form。我想念什么?这个错误是什么?
`<class:LoginTest>': undefined local variable or method `login_form_path' for LoginTest:Class (NameError)
在我的login_test.rb中,我有:
require 'test_helper'
class LoginTest < ActionDispatch::IntegrationTest
setup do
@bob = user(:bob)
end
test 'bob can login' do
get login_form_path
assert_response :success
login(@bob.email, 'abc123')
assert_template 'show_user'
end
test 'bob cannot login with bad password'
get login_form_path
assert_response :success
login(@bob.email, 'bad_password')
assert_template 'login_form'
end
在我的路由中:
Rails.application.routes.draw do
resources :logins
resources :users
root 'html#login_form'
get '/login', to: 'html#login_form', as: :login_form
post '/login', to: 'sessions#create_session', as: :create_session
get '/logout', to: 'sessions#destroy_session', as: :destroy_session
get '/users/:id', to: 'html#show_user', as: :show_user
get '/accounts/:id', to: 'html#show_account', as: :show_account
end
您在测试中缺少do
:test 'bob cannot login with bad password'
test 'bob cannot login with bad password' do
get login_form_path
assert_response :success
login(@bob.email, 'bad_password')
assert_template 'login_form'
end
有关更多信息,请参见ActionDisPatch :: IntegrationTest API文档的第一个示例