【发布时间】:2015-09-01 22:32:42
【问题描述】:
我正在学习 Rails,并且对测试非常陌生,但到目前为止,我已经设法构建了一些错误最少的东西。但是,我遇到的问题是我的测试抱怨找不到方法并且没有路由匹配。
据我了解,测试应该经常基于Hartl's - Railstutorial 3.3 运行。许多 StackO 线程和在线文章似乎都与使用我不使用的测试套件(如 RSpec 等)有关……因此测试配置令人困惑。我的测试套件的设置类似于Hartl's - Railstutorial 3.3,下面是为测试加载的 gem。
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>
Rake 路线 与汽车有关
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
我有什么遗漏或配置不正确吗?
请知道我的应用程序似乎工作正常,只是我想抑制这些测试错误。
请告知如何让这些测试错误通过。
谢谢
【问题讨论】:
标签: ruby-on-rails ruby devise minitest