简体   繁体   English

在Rails环境之外测试(使用RSpec)控制器

[英]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. 我正在创建一个gem,它将为将使用它的Rails应用程序生成一个控制器。 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 ). 当测试模型,它已经很容易的,但在测试控制器时,ActionController的:: TestUnit不包括(如描述在这里 )。 I've tried requiring it, and all similar sounding stuff in Rails but it hasn't worked. 我试过要求它,以及Rails中所有类似的东西,但没有用。

What would I need to require in the spec_helper to get the test to work? 我需要在spec_helper中要求什么才能使测试生效?

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. 这是一个工作正常的Test :: Unit测试示例,其中包含一个受测试的简单控制器。也许这里有些部分需要转移到rspec代码。

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

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

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