Export to GitHub

moq - issue #379

Issue on Dispose when using a Mock on WebClient and Strict behavior


Posted on Sep 2, 2014 by Grumpy Dog

What steps will reproduce the problem? 1. Initialize a new Mock<WebClient>(MockBehavior.Strict): Mock<WebClient> mockWebClient = new Mock<WebClient>(MockBehavior.Strict); 2. Setup the protected virtual Dispose method : mockWebClient.Protected().Setup("Dispose", ItExpr.IsAny<bool>()); 3. Call the Dispose method: using (WebClient client = mockWebClient.Object)) { }

What is the expected output? What do you see instead? It should work without error. But starting with the version 4.2.1408.619, I have an exception: Test method XXX threw exception: Moq.MockException: IDisposable.Dispose() invocation failed with mock behavior Strict. All invocations on the mock must have a corresponding setup. Result StackTrace:
at Moq.ExtractProxyCall.HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) at Moq.Interceptor.Intercept(ICallContext invocation) at Moq.Proxy.CastleProxyFactory.Interceptor.Intercept(IInvocation invocation) at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.WebClientProxy.Dispose() at ...

The problem being that I can't mock the Dispose() method, it's not virtual. The Dispose method calls the protected virtual Dispose(bool) method, so my way should be the correct one.

Previously, I was using the version 4.2.1312.1622 and it was working correctly. I tested all versions available on Nuget from 4.2.1312.1622 to 4.2.1408.717 (latest at the moment), and it stopped working at the version 4.2.1408.619.

What version of the product are you using? On what operating system? Works correctly with nuget versions: 4.2.1312.1622, 4.2.1402.2112 Doesn't work with nuget versions: 4.2.1408.619, 4.2.1408.717 (latest ATM) My operating system is Windows 7, I tested with Frameworks 4, 4.5 and 4.5.1

Please provide any additional information below. /

Thank you

Status: New

Labels:
Type-Defect Priority-Medium