I recently developed a Silverlight application that uses Mark J Millers ClientChannelWrapper to communicate with the WCF service level (effectively killing a service link and wrapping IClientChannel and ClientChannelFactory). Here is the interface:
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); }
Wrapper mainly uses a common asynchronous service interface (which can be generated by slsvcutil or manually processed after WCF ServiceContract) and terminates calls to ensure that a new channel is created in the event of a channel failure. A typical use is as follows:
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); }
Everything works fine, but now I would like to unit test view models that use ClientChannelWrapper. I installed a simple unit test using Moq:
[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); }
I get a NotSupportedException: Unsupported expression: p => p.BeginGetCurrentUser (IsAny (), null). I am new to Moq, but I think there are some problems with ClientChannelWrapper using common service interfaces. Trying to wrap your head around this for quite some time, maybe someone has an idea. Thanks.
source share