我最近開發了一個Silverlight應用程序,它使用Mark J Millers ClientChannelWrapper與WCF服務層通信(有效地終止服務引用並打包IClientChannel和ClientChannelFactory)。 這裏是接口:Mocking泛型WCF ClientChannelWrapper異步調用
public interface IClientChannelWrapper<T> where T : class
{
IAsyncResult BeginInvoke(Func<T, IAsyncResult> function);
void Dispose();
void EndInvoke(Action<T> action);
TResult EndInvoke<TResult>(Func<T, TResult> function);
}
包裝物基本上採用一個通用異步服務接口(其可能已經被slsvcutil或手的WCF的ServiceContract後製作的產生)和包裹的調用,以確保在一個信道的情況下故障,會創建一個新頻道。 典型用法是這樣的:
public WelcomeViewModel(IClientChannelWrapper<IMyWCFAsyncService> service)
{
this.service = service;
this.synchronizationContext = SynchronizationContext.Current ?? new SynchronizationContext();
this.isBusy = true;
this.service.BeginInvoke(m => m.BeginGetCurrentUser(new AsyncCallback(EndGetCurrentUser), null));
}
private void EndGetCurrentUser(IAsyncResult result)
{
string strResult = "";
service.EndInvoke(m => strResult = m.EndGetCurrentUser(result));
this.synchronizationContext.Send(
s =>
{
this.CurrentUserName = strResult;
this.isBusy = false;
}, null);
}
這一切工作正常,但現在我想進行單元測試,其使用ClientChannelWrapper視圖模型。 我已經設置了起訂量使用一個簡單的單元測試:
[TestMethod]
public void WhenCreated_ThenRequestUserName()
{
var serviceMock = new Mock<IClientChannelWrapper<IMyWCFAsyncService>>();
var requested = false;
//the following throws an exception
serviceMock.Setup(svc => svc.BeginInvoke(p => p.BeginGetCurrentUser(It.IsAny<AsyncCallback>(), null))).Callback(() => requested = true);
var viewModel = new ViewModels.WelcomeViewModel(serviceMock.Object);
Assert.IsTrue(requested);
}
我得到NotSupportedException異常:不支持的表達式:P => p.BeginGetCurrentUser(IsAny(),空)。我對Moq很新,但我猜想使用通用服務接口的ClientChannelWrapper存在一些問題。試圖把我的頭圍繞這個相當長一段時間,也許有人有一個想法。謝謝。
不要遺憾回答你的問題。在其他人提供可能的解決方案之前,您找到了可以解決問題的答案。爲什麼你不應該與其他面臨類似問題的人分享這個答案? – froeschli 2011-02-27 12:37:59