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

Verify passes on strict mock for parameter without proper setup. #285

Closed
martin-stepanek001 opened this issue Sep 16, 2016 · 2 comments
Closed

Comments

@martin-stepanek001
Copy link

Normally it would fail on MockException, but if that is caught, Verify will pass.
See following code:

[TestClass]
public class StrictVerify
{
    [TestMethod]
    public void Test()
    {
        var mock = new Mock<IToMock>(MockBehavior.Strict);
        mock.Setup(m => m.Action("mistake"));
        var sut = new Sut();
        sut.DoSomething(mock.Object, "action"); // won't throw MockException due to catch inside.
        mock.Verify(m => m.Action("action"), Times.Once()); //passes
    }
}

public interface IToMock
{
    void Action(string param);
}

public class Sut
{
    public void DoSomething(IToMock obj, string param)
    {
        try
        {
            obj.Action(param);
        }
        catch(Exception)
        {
            //handle exception without rethrow.
        }
    }
}

I would expect Verify to respect MockBehavior.Strict and fail in this case, because even though invocation was performed once, it was prohibited by mock and should not count as correct one.

@kzu
Copy link
Contributor

kzu commented Sep 16, 2016

Swallowing all exceptions is hardly a good practice. Your code should only handle what it can recover from. It's unlikely it can recover from anything, like a ThreadAbort, OutOfMemory or the like. Likewise, it shouldn't be able to recover from a MockExcepcion

@stakx stakx closed this as completed Jun 4, 2017
@stakx
Copy link
Contributor

stakx commented Jun 4, 2017

(Closing this issue, since there's really nothing that can nor should be done about this.)

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

3 participants