簡體   English   中英

在Rails環境之外測試(使用RSpec)控制器

[英]Test (with RSpec) a controller outside of a Rails environment

我正在創建一個gem,它將為將使用它的Rails應用程序生成一個控制器。 在嘗試測試控制器時,這對我來說是一個反復試驗的過程。 當測試模型,它已經很容易的,但在測試控制器時,ActionController的:: TestUnit不包括(如描述在這里 )。 我試過要求它,以及Rails中所有類似的東西,但沒有用。

我需要在spec_helper中要求什么才能使測試生效?

謝謝!

這是一個工作正常的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