0
我有一個服務代理類,使asyn調用服務操作。我使用回調方法將結果傳回給我的視圖模型。RhinoMocks測試回調方法
做視圖模型的功能測試,我可以嘲笑服務代理,以確保在代理上調用方法,但我怎樣才能確保調用回調方法呢?
隨着RhinoMocks我可以測試事件處理和模擬對象上的事件引發事件,但我怎麼測試回調?
視圖模型:
public class MyViewModel
{
public void GetDataAsync()
{
// Use DI framework to get the object
IMyServiceClient myServiceClient = IoC.Resolve<IMyServiceClient>();
myServiceClient.GetData(GetDataAsyncCallback);
}
private void GetDataAsyncCallback(Entity entity, ServiceError error)
{
// do something here...
}
}
ServiceProxy:
public class MyService : ClientBase<IMyService>, IMyServiceClient
{
// Constructor
public NertiAdminServiceClient(string endpointConfigurationName, string remoteAddress)
:
base(endpointConfigurationName, remoteAddress)
{
}
// IMyServiceClient member.
public void GetData(Action<Entity, ServiceError> callback)
{
Channel.BeginGetData(EndGetData, callback);
}
private void EndGetData(IAsyncResult result)
{
Action<Entity, ServiceError> callback =
result.AsyncState as Action<Entity, ServiceError>;
ServiceError error;
Entity results = Channel.EndGetData(out error, result);
if (callback != null)
callback(results, error);
}
}
感謝
請告訴我如何使內部方法可見單元測試項目。謝謝 – joblot 2010-06-01 05:53:56
我已經制定了如何使內部方法可見。 能否請你解釋一下實際做了什麼以及爲什麼我們爲服務創建了一個螺柱。謝謝 – joblot 2010-06-01 06:04:08
這只是你的ViewModel的單元測試。因此,所有外部依賴都被嘲弄/殘留。 「Do」方法告訴Rhino.Mocks在調用方法時執行特定的代碼片段。我使用了「Do」,這樣你的ViewModel就可以調用「GetDataAsync」並且還可以獲得回調 - 所有這些都在測試期間完成。 測試服務是否執行回調將作爲Service類的單元測試的一部分完成。 – PatrickSteele 2010-06-01 14:22:28