Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
MarienMonnier opened this issue Sep 2, 2014 · 3 comments
Closed

Comments

@MarienMonnier
Copy link

What steps will reproduce the problem?

Using this code :

Mock<WebClient> mockWebClient = new Mock<WebClient>(MockBehavior.Strict);
mockWebClient.Protected().Setup("Dispose", ItExpr.IsAny<bool>());
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.

What version of the product are you using? On what operating system?
Works correctly with versions (from nuget): 4.2.1312.1622, 4.2.1402.2112
Doesn't work with 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

@drieseng
Copy link
Contributor

drieseng commented Oct 4, 2014

You should be using this:

mockWebClient.As< IDisposable >().Setup(p => p.Dispose());

@kzu
Copy link
Contributor

kzu commented Oct 11, 2014

Indeed.

@kzu kzu closed this as completed Oct 11, 2014
dougbu added a commit to aspnet/AspNetWebStack that referenced this issue Aug 25, 2016
- work around devlooped/moq#129 and devlooped/moq#212; Moq creates proxies for all `interface` methods
 - e.g. set up `IDisposable.Dispose()` calls
 - related bug devlooped/moq#212 means we can't use `MockBehavior.Strict` in some cases
  - especially when method returns a value and therefore can't be set up to call base
  - `CallBase = true` is often required in these cases
- work around devlooped/moq#149; `DefaultValue.Mock` restrictions
 - where necessary, explicitly set up members instead
- work around odd failures in `JsonResultTest` using `SetupSet()` on deep properties
 - use `MockBehavior.Strict` in `NullContentIsNotOutput()` to confirm `Write()` is not called
- handle less-predictable proxy type names
dougbu added a commit to aspnet/AspNetWebStack that referenced this issue Aug 26, 2016
- #11 (3 of 4)
- work around devlooped/moq#129 and devlooped/moq#212; Moq creates proxies for all `interface` methods
 - e.g. set up `IDisposable.Dispose()` calls
 - related bug devlooped/moq#212 means we can't use `MockBehavior.Strict` in some cases
  - especially when method returns a value and therefore can't be set up to call base
  - `CallBase = true` is often required in these cases
- work around devlooped/moq#149; `DefaultValue.Mock` restrictions
 - where necessary, explicitly set members up instead
- work around odd failures in `JsonResultTest` using `SetupSet()` on deep properties
 - use `MockBehavior.Strict` in `NullContentIsNotOutput()` to confirm `Write()` is not called
- handle less-predictable proxy type names
@aleks091
Copy link

Thanks ! @drieseng

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants