下面是一個寶石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環境。特別是如果我想設置兩個應用程序TestTailsApp1
和TestTailsApp2
。有什麼建議?
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