Rails Test w/ Minitest Reporter - NoMethodError: undefined method + ActionController::UrlGenerationError: No route matches

1.2k views Asked by At

I am learning Rails and very new to testing but so far I've managed to build something with minimal errors. However, the issue I am running into is that my tests are complaining for methods that cannot be found and no route matching.

To my understanding tests should be run frequently based Hartl's - Railstutorial 3.3. Many StackO threads and online articles seem to pertain to utilizing test suites I don't use like RSpec, etc...so the test configurations are confusing. My testing suite is set up similiar to Hartl's - Railstutorial 3.3 and below are the gems loaded for testing.

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: undefined method

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>"

Services Controller

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

I believe the reason I am receiving this error is because of Devise. If I were to setup the following index action below, the test will pass.

def index
  @services = Tech.first.services
end

How do I correct this so that this test passes?


ActionController::UrlGenerationError: No route matches {: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 routes pertaining to cars

tech_cars POST - /techs/:tech_id/cars(.:format) - cars#create
car GET - /cars/:id(.:format) - cars#show

Routes

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 (only links on page)

<%= 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) %>

As you may gather, I am building a Garage not a Car object within the Cars controller. Would this be a problem for tests? My application functions fine as is. On a side note, I am also having difficulty trying to associate car_params strong params for instance variables but that's another StackO post.


*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

Is there something I am missing or haven't configured correctly?

Please know my application appears to be working fine, it's just I would like to suppress these tests errors.

Please advise on how to get these test errors to pass.

Thanks

1

There are 1 answers

3
Chris Kottom On

First issue: Your feeling is correct. Devise comes with Devise::TestHelpers which you've mixed into ActionController::TestCase in your test helper file. One of the methods it provides is sign_in which lets you spoof a logged in user as part of your test. Assuming you've got a model called Tech and you're following standard Rails conventions, you'll need to add something like:

sign_in techs(:some_tech)

to either your setup block or directly into your test body before the call to get :index. That will ensure that current_tech returns something non-nil and remove the immediate NoMethodError.

Second issue: Your :show action expects to receive the ID of a known Car as part of the URL. Replace your current invocation of the controller with:

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