简体   繁体   English

Minitest - 测试控制器问题

[英]Minitest - test controller concerns

I try to test my controller concerns using minitest-rails and combining this techniques:我尝试使用 minitest-rails 并结合以下技术来测试我的控制器问题:

http://ridingtheclutch.com/post/55701769414/testing-controller-concerns-in-rails . http://ridingtheclutch.com/post/55701769414/testing-controller-concerns-in-rails

Anonymous controller in Minitest w/ Rails Minitest 中的匿名控制器 w/Rails

And i get "no route matches error": ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"fake"}我得到“没有路由匹配错误”: ActionController::UrlGenerationError: 没有路由匹配 {:action=>"index", :controller=>"fake"}

require "test_helper"
require "warden_mock"

class FakeController < ApplicationController
  attr_accessor :request

  def initialize(method_name=nil, &method_body)
    include MyConcern # method redirect_to_404 placed here

    @request = OpenStruct.new # mockup request
    @request.env = {}
    @request.env['warden'] = WardenMock.new # mockup warden

    if method_name and block_given? # dynamically define action for concern methods testing
      self.class.send(:define_method, method_name, method_body)
      test_routes = Proc.new do
        resources :fake
      end
      Rails.application.routes.eval_block(test_routes)
    end
  end
end


describe FakeController do # just very simple test
  context "just redirect_to_404" do
    it "it must redirect to /404" do
      @controller = FakeController.new(:index) { redirect_to_404 }
      get :index
      assert_redirected_to '/404'
    end
  end
end

I have rails 4.1.5 and minitest 5.4.0我有 rails 4.1.5 和 minitest 5.4.0

Probably too late for the OP, but I've done it in this way:对于 OP 来说可能为时已晚,但我是这样做的:

require 'test_helper'

class SolrSearchParamsFakeController < ApplicationController
  include SolrSearchParams # this is my controller's concern to test

  def index
    # The concern modify some of the parameters, so I'm saving them in a
    # variable for future test inspection, so YMMV here.
    @params = params
    render nothing: true
  end
end

Rails.application.routes.draw do
  # Adding a route to the fake controller manually
  get 'index' => 'solr_search_params_fake#index'
end

class SolrSearchParamsFakeControllerTest < ActionController::TestCase
  def test_index
    get :index, search: 'asdf wert'

    # finally checking if the parameters were modified as I expect
    params = assigns(:params)
    assert_equal params[:original_search], 'asdf wert'
  end
end

Update更新

Sorry, but actually this is messing up all my tests that involve route access in some way, as with Rails.application.routes.draw I'm rewriting all the routes for tests and leaving just that solr_search_params_fake#index route.抱歉,但实际上这在某种程度上搞乱了我所有涉及路由访问的测试,就像Rails.application.routes.draw一样,我正在重写所有测试路由,只留下solr_search_params_fake#index路由。
Not sure how to add instead of rewriting.... a fix would be adding directly to config/routes.rb my test routes with an if Rails.env.test?不知道如何添加而不是重写....一个修复将直接添加到config/routes.rb我的测试路由与一个if Rails.env.test? condition?健康)状况? (yeah, it's a crappy solution, but I'll leave this here in case someone find a better way to do this). (是的,这是一个糟糕的解决方案,但我会把它留在这里,以防有人找到更好的方法来做到这一点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM