2010-04-05 89 views
4

我正在創建一個gem,它將爲將使用它的Rails應用程序生成一個控制器。試圖測試控制器時,這對我來說是一個試驗和錯誤的過程。在測試模型時,它非常容易,但是在測試控制器時,不包括ActionController :: TestUnit(如描述here)。我試過要求它,以及Rails中所有類似的聲音,但它沒有奏效。在Rails環境之外測試(使用RSpec)控制器

在spec_helper中需要什麼才能使測試正常工作?

謝謝!

+0

我從小在rspec的用戶這個問題的郵件列表,並Aslak Hellesoy說,一般情況下,如果你正在測試一個Rails的事情,它更容易編寫的寶石,在Rails應用程序使用它,在Rails應用程序中編寫這些測試。雖然聽起來很容易,但它感覺就像是分開了。當我在野外放置這顆寶石時,它會變得脆弱。 – 2010-04-06 15:27:54

+0

從那時起,我選擇使用「虛擬」應用程序方法。請參閱http://whilefalse.net/2012/01/25/testing-rails-engines-rspec/ – 2012-10-23 13:57:25

回答

2

下面是一個工作的獨立測試::單元測試示例,包含一個簡單的包含測試的控制器..也許這裏有一些部分需要傳輸到您的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 
+0

謝謝我今天晚些時候會嘗試這種方式,併發布它的方式。 – 2010-04-06 02:28:16

+0

看來最基本的區別是我不認爲你可以讓你的測試類成爲rspec中ActionController :: TestCase的一個子類。在rspec中我相信它是基於describe塊自動推斷的。例如。描述SampleController,「在GET索引上」將在app/controllers中尋找SampleController。我希望我錯了,這可以被覆蓋! – 2010-04-06 15:25:18

相關問題