我正在阅读Sam Ruby的Rails 5敏捷开发。 https://pragprog.com/book/rails51/agile-web-development-with-rails-5-1
在第14章任务I:使用Rails测试后登录,控制台出现以下错误
Error:
ProductTest#test_image_url:
NameError: undefined local variable or method `login_url' for #<ProductTest:0x00000000062aadb8>
Did you mean? login_as
test/test_helper.rb:10:in `login_as'
test/test_helper.rb:16:in `setup'
bin/rails test test/models/product_test.rb:38
E
Error:
ProductTest#test_product_price_must_be_positive:
NameError: undefined local variable or method `login_url' for #<ProductTest:0x0000000005b9b068>
Did you mean? login_as
test/test_helper.rb:10:in `login_as'
test/test_helper.rb:16:in `setup'
bin/rails test test/models/product_test.rb:15
E
Error:
ProductTest#test_product_is_not_valid_without_a_unique_title:
NameError: undefined local variable or method `login_url' for #<ProductTest:0x0000000008349948>
Did you mean? login_as
test/test_helper.rb:10:in `login_as'
test/test_helper.rb:16:in `setup'
bin/rails test test/models/product_test.rb:50
E
Error:
ProductTest#test_product_attributes_must_not_be_empty:
NameError: undefined local variable or method `login_url' for #<ProductTest:0x0000000007ff8c08>
Did you mean? login_as
test/test_helper.rb:10:in `login_as'
test/test_helper.rb:16:in `setup'
bin/rails test test/models/product_test.rb:6
.......E
Error:
UserStoriesTest#test_buying_a_product:
ActionView::Template::Error: undefined method `each' for nil:NilClass
app/views/store/index.html.erb:7:in `block in _app_views_store_index_html_erb__32180898452112054_53581700'
app/views/store/index.html.erb:6:in `_app_views_store_index_html_erb__32180898452112054_53581700'
test/integration/user_stories_test.rb:16:in `block in <class:UserStoriesTest>'
我尝试在线搜索解决方案,但找不到。Agile Web Development Rails 5 test failures这是一个具有相同错误的类似问题,但我不了解解决方案
我的测试/test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
def login_as(user)
post login_url, params: { name: user.name, password: 'secret' }
end
def logout
delete logout_url
end
def setup
login_as users(:one)
end
# Add more helper methods to be used by all tests here...
end
我的配置/routes.rb
Rails.application.routes.draw do
get 'admin' => 'admin#index'
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
resources :users
resources :orders
resources :line_items
resources :carts
root 'store#index', as: 'store_index'
resources :products do
get :who_bought, on: :member
end
end
在本教程中,login_url
旨在作为Rails定义的URL帮助器,基于routes.rb
中的这一行:
controller :sessions do
get 'login' => :new
end
所有路线都生成了一个帮助程序来访问它们,您可以使用rails routes -G login
复制该信息。在您的情况下,这将显示以下内容:
login GET /login(.:format) sessions#new
这显示的是1)路由名称和HTML方法,2)路径和3)处理它的controller#action
。>
在上面的示例中,login_url
将起作用,因为由于自动生成的帮助程序,路由名称可以以_url
或_path
后缀。
但是,由于遇到问题,您可能不会完全看到此情况。您可能会看到例如:
sessions GET /login(.:format) sessions#new
这意味着您有两个选择:
使用sessions_url
代替login_url
。
或者,也许是正确的解决方案,使用routes.rb
在as
中命名您的路线:
controller :sessions do get 'login' => :new, as: 'login' end
执行此操作,您的路线应重命名为
login
,因此可以使用login_url
。
让我知道您的相处方式或是否有任何疑问:)