如何使用Shoulda测试应用程序控制器方法?

时间:2014-04-08 16:27:18

标签: ruby-on-rails rails-api

我在应用程序控制器中测试内容的最近资源是here。不幸的是,它实际上并不起作用 - MyApp会生成一个未初始化的常量。我尝试过添加适当路由的其他方法,但都没有。

我的目的是测试我嵌入到ApplicationController中的身份验证方法 - 我不想将测试放到其他控制器中,因为这不是他们关注的问题。这是支持他们的东西,但它不是'他们'功能的一部分。     要求'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

上面的代码导致url helper方法错误:

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

编辑:

感谢博尔德的善意建议,我提出过这个建议,但他之后修复路线的建议不起作用:

teardown do
  Rails.application.reload_routes!
end

1 个答案:

答案 0 :(得分:2)

假设MyApp是您的应用程序的名称,我怀疑。您应该更改它并使用您的应用程序的实际名称。更好的是,做

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