Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minitest - test controller concerns

I try to test my controller concerns using minitest-rails and combining this techniques:

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

Anonymous controller in Minitest w/ Rails

And i get "no route matches error": ActionController::UrlGenerationError: No route matches {: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

like image 674
rootatdarkstar Avatar asked Sep 19 '14 15:09

rootatdarkstar


People also ask

How to test Rails controllers?

The currently accepted way to test rails controllers is by sending http requests to your application and writing assertions about the response. Rails has ActionDispatch::IntegrationTest which provides integration tests for Minitest which is the Ruby standard library testing framework.

How to run test cases Rails?

We can run all of our tests at once by using the bin/rails test command. Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case.

What is unit test in Rails?

The Tests − They are test applications that produce consistent result and prove that a Rails application does what it is expected to do. Tests are developed concurrently with the actual application. The Assertion − This is a one line of code that evaluates an object (or expression) for expected results.


1 Answers

Probably too late for the OP, but I've done it in this way:

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.
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? condition? (yeah, it's a crappy solution, but I'll leave this here in case someone find a better way to do this).

like image 194
Alter Lagos Avatar answered Sep 21 '22 21:09

Alter Lagos