【问题标题】:Creating a Dummy Controller for Rails Concerns not working为 Rails 问题创建一个虚拟控制器不起作用
【发布时间】:2014-10-08 03:38:49
【问题描述】:

我正在尝试基于this example动态创建一个控制器来测试我的rails控制器问题。

我的代码是这样的

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

然而,每当我尝试运行代码时,我都会收到错误消息

RequireAuthenticationTest#test_Throws_token_required_error: ActionController::UrlGenerationError:没有路由匹配 {:action=>"index", :controller=>"require_authentication_test"} test/controllers/concerns/require_authentication_test.rb:7:in `block in '

关于我做错了什么有什么建议吗?谢谢!

【问题讨论】:

  • 您也可以发布您的 RequireAuthentication 代码吗?
  • 我认为这无关紧要。问题是控制器路由没有被识别。我的 RequireAuthentication 代码甚至没有被调用。
  • 哼。我在尝试运行博客文章中的代码时遇到错误。你用的是哪个版本的 Rails 和 mini test?
  • Rails 4.1.6 和 Minitest 5.4.2
  • 你最终弄明白了吗?如果是这样,你会这么好心回答你自己的问题。我现在正在查看确切的一些问题。

标签: ruby-on-rails ruby ruby-on-rails-4 minitest


【解决方案1】:

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

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

...但我无法解释为什么会这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 2016-06-13
    • 2012-12-19
    • 2020-10-03
    • 2017-01-02
    相关资源
    最近更新 更多