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

Mock<Class : IInterface>: Inconsistency between mocked-behavior and verification #157

Closed
BrunoJuchli opened this issue Feb 18, 2015 · 1 comment
Labels

Comments

@BrunoJuchli
Copy link

I've detected the behavior in Moq 4.2.1502.0911, and it's probably related to #156

Let's consider Stream as an example, see the relevant excerpts of the implementation of Stream:

public class Stream : IDisposable
{
    public void Dispose()
    { 
        Close(); 
    }

    public virtual void Close()
    {
     .....
    }
}

Note: Dispose() does not have the virtual identifier (but is inherited from an interface) and Close is virtual.

I've used to test the usage of a Stream as follows:

var streamMock = new Mock<Stream>();

using(var stream = streamMock.Object) { }

// non-mocked Dispose methods calls Close which is virtual and can thus be verified
streamMock.Verify(x => x.Close()); 

However, with the current Moq version this fails, resulting in the following error:

Expected invocation on the mock once, but was 0 times: x => x.Close()
No setups configured.

Performed invocations:
IDisposable.Dispose()

so actually Mock mocks (intercepts) IDispoable.Dispose(). But verifying that Dispose was called fails:

streamMock.Verify(x => x.Dispose());

results in:

System.NotSupportedException : Invalid verify on a non-virtual (overridable in VB) member: x => x.Dispose()

I've found a workaround, which is:

streamMock.As<IDisposable>().Verify(x => x.Dispose());

However i think the current behavior is inconsistent.
I don't mind Moq intercepting the Dispose method, but then i would expect streamMock.Verify(x => x.Dispose()) to pass.

@stakx
Copy link
Contributor

stakx commented Jun 20, 2017

Fixed in Moq 4.7.58.

@stakx stakx closed this as completed Jun 20, 2017
@devlooped devlooped locked and limited conversation to collaborators Sep 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants