![](/img/trans.png)
[英]ActionController::UrlGenerationError No route matches AND NoMethodError undefined method `accounts_lists_url' with nested resources
[英]Rails Test w/ Minitest Reporter - NoMethodError: undefined method + ActionController::UrlGenerationError: No route matches
我正在學習Rails,並且對測試非常陌生,但是到目前為止,我已經成功地構建了一些錯誤最少的東西。 但是,我遇到的問題是我的測試抱怨找不到方法且沒有路由匹配的方法。
據我了解,應該經常根據Hartl's-Railstutorial 3.3進行測試 。 許多StackO線程和在線文章似乎都與利用我不使用的測試套件(如RSpec等)有關,因此測試配置令人困惑。 我的測試套件的設置類似於Hartl的-Railstutorial 3.3 ,以下是用於測試的工具。
gem 'better_errors', '~> 2.1.1'
gem 'binding_of_caller'
gem 'minitest-reporters', '1.0.5'
gem 'mini_backtrace', '0.1.3'
gem 'guard-minitest', '2.3.1'
gem 'ruby-prof'
NoMethodError:未定義的方法
Error: (_I have two error similar errors to below_)
ServicesControllerTest#test_should_get_index:
NoMethodError: undefined method 'services' for nil:NilClass
app/controllers/services_controller.rb:6:in 'index'
test/controllers/services_controller_test.rb:9:in 'block in <class:ServicesControllerTest>"
服務主管
def index
@services = current_tech.services
end
services_controller_test.rb
require 'test_helper'
class ServicesControllerTest < ActionController::TestCase
setup do
@service = services(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:services)
end
end
我相信我收到此錯誤的原因是因為Devise
。 如果我要在下面設置以下索引操作,則測試將通過。
def index
@services = Tech.first.services
end
我該如何糾正此問題以便通過測試?
ActionController :: UrlGenerationError:沒有路由匹配{:action =>“ show”,:controller =>“ tech”}
Error:
CarsControllerTest#test_should_get_show:
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"cars"}
test/controllers/cars_controller_test.rb:5:in `block in <class:CarsControllerTest>
與汽車有關的 耙路
tech_cars POST - /techs/:tech_id/cars(.:format) - cars#create
car GET - /cars/:id(.:format) - cars#show
路線
Rails.application.routes.draw do
devise_for :customers, controllers: { sessions: 'customers/sessions' }
devise_for :techs, controllers: { sessions: 'techs/sessions' }
resources :techs, :only => [:index, :show], shallow: true do
resources :cars, only: [:show, :create]
end
resources :services, :garages
root "home#index"
end
cars_controller.rb
class CarsController < ApplicationController
before_action :set_car
def show
@garage = Garage.find(params[:id])
@tech = @tech.service.car.id
end
def create
@garage = Garage.create(tech_first_name: @car.service.tech.first_name,
customer_id: current_customer.id,
customer_street_address: current_customer.street_address,
customer_city: current_customer.city,
customer_state: current_customer.state,
customer_zip_code: current_customer.zip_cod)
if @garage.save
redirect_to techs_path, notice: "Working"
else
redirect_to techs_path, notice: "Uh oh, flat tire"
end
end
private
def set_car
@car = Car.find(params[:id])
end
def car_params
params.permit(:service_name, :garage_photo)
end
end
cars_controller_test.rb
require 'test_helper'
class CarControllerTest < ActionController::TestCase
test "should get show" do
get :show
assert_response :success
end
end
cars.html.erb (僅頁面上的鏈接)
<%= button_to 'View Garage', tech_cars_path(tech_id: @car.service.tech.id, id: @car) %>
<%= link_to 'Back to tech', tech_path(@car.service.tech.id) %>
就像您可能會收集到的那樣,我正在Cars
控制器中構建Garage
而不是Car
對象。 測試會不會有問題? 我的應用程序按原樣運行正常。 附帶一提,我也很難嘗試將car_params
強參數關聯到實例變量,但這是StackO的另一篇文章。
* test / test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
class ActionController::TestCase
include Devise::TestHelpers
end
是否有我缺少或配置不正確的東西?
請知道我的應用程序似乎運行正常,只是我想抑制這些測試錯誤。
請告知如何使這些測試錯誤通過。
謝謝
第一個問題:您的感覺是正確的。 Devise隨附了Devise::TestHelpers
,您已將其混合到測試幫助文件中的ActionController::TestCase
中。 它提供的一種方法是sign_in
,它使您可以在測試過程中欺騙已登錄的用戶。 假設您有一個名為Tech
的模型,並且遵循標准的Rails約定,則需要添加以下內容:
sign_in techs(:some_tech)
調用get :index
之前,可以進入setup
塊或直接進入測試主體。 這將確保current_tech
返回非零值,並刪除立即的NoMethodError。
第二個問題:您的:show
動作希望收到一個已知Car
的ID作為URL的一部分。 將您當前對控制器的調用替換為:
require 'test_helper'
class CarControllerTest < ActionController::TestCase
setup do
@car = cars(:some_car)
end
test "should get show" do
get :show, id: @car.id
assert_response :success
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.