Test (with RSpec) a controller outside of a Rails environment

前端 未结 1 1891
再見小時候
再見小時候 2021-01-06 05:07

I\'m creating a gem that will generate a controller for the Rails app that will use it. It\'s been a trial and error process for me when trying to test a controller. When te

相关标签:
1条回答
  • 2021-01-06 05:46

    Here's an example of a working standalone Test::Unit test with a simple controller under test included.. Maybe there's some parts here that you need to transfer over to your rspec code.

    require 'rubygems'
    require 'test/unit'
    require 'active_support'
    require 'active_support/test_case'
    require 'action_controller'
    require 'action_controller/test_process'
    
    class UnderTestController < ActionController::Base
      def index
        render :text => 'OK'
      end
    end
    ActionController::Routing::Routes.draw {|map| map.resources :under_test }
    
    class MyTest < ActionController::TestCase
      def setup
        @controller = UnderTestController.new
        @request    = ActionController::TestRequest.new
        @response   = ActionController::TestResponse.new
      end
    
      test "should succeed" do
        get :index
        assert_response :success
      end
    end
    
    0 讨论(0)
提交回复
热议问题