如何測試模塊的這個小部分,超級? (超是action_dispatch-3.0.1測試/集成...)的模塊包含的投機/請求中攔截後:RSpec模擬或存根超模型
module ApiDoc
def post(path, parameters = nil, headers = nil)
super
document_request("post", path, parameters, headers) if ENV['API_DOC'] == "true"
end
...
end
我不希望它運行ActionDispatch ::集成 - 無論如何,但我不知道how to mock or stub super
單元測試它。
該模塊僅在規範中使用,並且將具有100%的測試覆蓋率,這證明這些度量類型是無用的。我需要單元測試它。
一個例子,如果需要的話,這是我如何使用模塊ApiDoc
require 'spec_helper'
describe "Products API" do
include ApiDoc ############## <---- This is my module
context "POST product" do
before do
@hash = {:product => {:name => "Test Name 1", :description => "Some data for testing"}}
end
it "can be done with JSON" do
valid_json = @hash.to_json
############### the following 'post' is overriden by ApiDoc
post("/products.json",valid_json,
{"CONTENT_TYPE" => "application/json",
"HTTP_AUTHORIZATION" => ActionController::HttpAuthentication::Basic.encode_credentials("user", "secret")})
response.should be_success
end
end
end
謝謝!我使用摩卡,所以這個工作:`ActionDispatch :: Integration :: Session.any_instance.expects(:post).with xx`,但是我不能模擬ApiDoc.post或-.document_request,就像它說的那樣它的類(ApiDoc)是`RSpec :: Core :: ExampleGroup :: Nested_1`如何在這種情況下嘲笑這些方法?或者我創建一個虛擬類來包含它,以測試這種行爲? – oma 2010-11-26 19:44:30