简体   繁体   中英

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

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 testing models, it's been pretty easy, but when testing controllers, ActionController::TestUnit is not included (as described here ). I've tried requiring it, and all similar sounding stuff in Rails but it hasn't worked.

What would I need to require in the spec_helper to get the test to work?

Thanks!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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