2013-07-09 69 views
4

我正在嘗試遵循this example並利用一個墊片來移除從我正在執行單元測試的方法調用的WCF服務調用的外部依賴。微軟提供WCF服務的墊片

ChannelFactory<IReportBroker> factory = new ChannelFactory<IReportBroker>("ReportBrokerBasicHttpStreamed", new EndpointAddress(this.CurrentSecurityZoneConfigurationManager.ConfigurationSettings[Constants.ConfigurationKeys.ReportBrokerServiceUrl])); 
IReportBroker proxy = factory.CreateChannel(); 
proxy.Execute(requestMessage)) 

如何適應這個例子勻場代理返回由CreateChannel方法:不像例子,我用類似這樣的代碼生成我的WCF客戶端對飛?我假設在ShimWCFService類,我需要添加類似....

ShimChannelFactory<TService>.AllInstances.CreateChannel = (var1) => { return [instance of a mock object]}; 

不過,我不確定如何的<TService>模擬對象與墊片作爲返回值相關聯。

+0

不同於論壇的網站,我們不使用的「謝謝」,或者「任何幫助表示讚賞」,或簽名(因此)。請參閱「[應該'嗨','謝謝',標語和致敬從帖子中刪除?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be -removed - 從 - 個)。 –

回答

3

您需要爲每個類型參數填充工廠。假設您有三個服務合同「IService0」,「IService1」和「IService2」。

然後,你需要設置墊片是這樣的:

ShimChannelFactory<IService0>.AllInstances.CreateChannel = (_) => { return new Service0Mock(); } 
ShimChannelFactory<IService1>.AllInstances.CreateChannel = (_) => { return new Service1Mock(); } 
ShimChannelFactory<IService2>.AllInstances.CreateChannel = (_) => { return new Service2Mock(); }