2014-10-08 36 views
1

我想动态地创建一个控制器来测试我的轨道控制器的关注,基于this example创建一个虚拟控制器的导轨问题不起作用

我的代码看起来像这样

require 'test_helper' 

class RequireAuthenticationTest < ActionController::TestCase 
    test "Throws token required error" do 
    @controller = RequireAuthenticationTestController.new(:index) { require_authentication; render :nothing => true } 
    get :index 

    assert_response :success 
    end 
end 

class RequireAuthenticationTestController < ActionController::Base 
    include RequireAuthentication 

    def initialize(method_name, &method_body) 
    self.class.send(:define_method, method_name, method_body) 
    end 
end 

每当我试着但是运行的代码,我得到了'

任何意见,错误​​块我在做什么错呢?谢谢!

+0

你可以发布你的RequireAuthentication代码吗? – 2014-10-08 03:45:03

+0

我不认为这是相关的。问题是控制器路由没有被识别。我的RequireAuthentication代码甚至没有被调用。 – 2014-10-08 03:49:28

+0

嗡嗡声。试图从博客文章运行代码时出现错误。您正在使用哪个版本的Rails和迷你测试? – 2014-10-08 04:01:20

回答

0

您可能需要为测试控制器定义路由。这是我用来测试我的面包屑的代码:

require 'test_helper' 
class BreadcrumbTest < ActionController::TestCase 

    test "Throws token required error" do 
    @controller = BreadcrumbTestsController.new(:index) { render :nothing => true } 

    with_routing do |set| 
     set.draw do 
     resources :breadcrumb_tests 
     end 

     begin 
     get :index 
     assert_response :success 
     end 

    end 
    end 
end 

class BreadcrumbTestsController < ActionController::Base 
    include BreadcrumbList 

    def initialize(method_name, &method_body) 
    self.class.send(:define_method, method_name, method_body) 
    end 

end 

...但我无法解释为什么这个工程。