我最近开发了一个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