2013-01-22 84 views
1

這個問題不是關於如何測試控制器在rails應用程序。gem框架集成測試:如何設置rspec控制器測試的寶石軌道集成

開發寶石我想測試我的寶石是否集成在軌道控制器中。 所以在gem根目錄下運行rspec是在rails環境之外的。

現在我該如何寫一個控制器測試,它可以使用rspec控制器示例組助手,如getpost

尤其是如果我設置一個例子組元標記:type => :controller,如何RSpec的設置Rails環境,我怎麼能鉤到,爲了說設立路線等

我寧願不需要設置大部分爲空的整個rails應用程序框架。但我甚至無法找到如何做到這一點的信息。測試gem集成到rails應用程序或多個框架的最佳實踐是什麼?

這些來源最接近來到我所追求的: Test (with RSpec) a controller outside of a Rails environment 但這是測試單元。 http://railsware.com/blog/2012/01/07/testing-gem-integration-with-multiple-ruby-frameworks/ 但這是用於直接掛在rails應用程序類中的水豚。

感謝所有

回答

1

下面是一個寶石Rails控制器集成測試的一個很好的小例子。 my_gem。假設你在一個簡單的rspec設置的gem根目錄下(比如說rspec --init)。然後spec/rails_controller_integration_spec.rb看起來像這樣。

rspec/rails是否需要,among them rspec/rails/example基於元標籤設置示例組類型。 metatag :type => :controller驅動包含適當的組模塊RSpec::Rails::ControllerExampleGroup,它爲您提供all the goodies of rails controller specing以及all the goodies of ActionController::TestCase like get/post

希望這會有所幫助。

我還沒有得到的是如何分配Rails環境。特別是如果我想設置兩個應用程序TestTailsApp1TestTailsApp2。有什麼建議?

require 'spec_helper' 
require 'my_gem' 

require 'rails' 
require 'action_controller/railtie' # allows ActionController::Base 
# crucial part here: 
require 'rspec/rails' 
# note that require 'rspec-rails' does not work 

module TestRailsApp 
    class Application < Rails::Application 
    # app config here 
    # config.secret_token = '572c86f5ede338bd8aba8dae0fd3a326aabababc98d1e6ce34b9f5' 
    # routes.draw do 
    # resources :models 
    # end 
    end 

    class ApplicationController < ActionController::Base 
    # setup 
    end 

end 

describe 'My gem' do 

    context "in a Rails controller", :type => :controller do 

    controller(TestRailsApp::ApplicationController) do 
     extend(RSpec::Rails::ControllerExampleGroup::BypassRescue) 
     # example-specific setup for anonymous controller 
     # https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/anonymous-controller 
     def index 
     end 
    end 

    before(:each) do 
     # request needs to be setup to avoid path setting error 
     @request = ActionController::TestRequest.new 
    end 

    describe "#index" do 
     it "works" do 
     get :index 
     response.body.should == 'index content' 
     end 
    end 
    end 

end