Pregunta

The closest resource I've found for testing things inside the application controller is here. Unfortunately, it doesn't actually work -- MyApp generates an uninitialized constant. I've tried other methods of adding the appropriate route, but none of them work.

My purpose is to test the authentication methods I've imbedded into the ApplicationController -- I don't want to put the tests into other controllers, because that's not their concern. It's something that supports them, but it's not part of 'their' functionality. require 'test_helper'

class TestController < ApplicationController
  def index; end
end

class AppControllerTest < ActionController::TestCase

  Rails.application.routes.draw do
    controller :test do
      get 'test/index' => :index
    end
  end

  setup do
    @controller = TestController.new
  end

  should "return nil if there is no district subdomain" do
    get :index
    assert_equal 'bar', @controller.send(:foo)
  end

end

The above code results in an url helper method error:

ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"test"}

Edit:

Thanks to Boulder's kind advice, I moved past this, but his suggestion for fixing the routes after doesn't work:

teardown do
  Rails.application.reload_routes!
end
¿Fue útil?

Solución

That's assuming that MyApp is the name of your application, which I doubt. You should change it and use the actual name of your application. Even better, do

    Rails.application.routes.append
      controller :test do
        get 'test/index' => :index
      end
    end
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top