devlooped / moq

The most popular and friendly mocking framework for .NET
Other
5.87k stars 800 forks source link

Fail message on VerifyNoOtherCalls #1263

Open tengl opened 2 years ago

tengl commented 2 years ago

I would like to have a message on tests when VerifyNoOtherCalls fails. It would be useful if you have a number of cases where the mock should not be called in a Theory.

Consider following (artificial) example, where it would be nice to have the failMsg printed in output if the test fails.

public class TestClass
{
    private readonly ITestInterface component;

    public TestClass(ITestInterface component)
    {
        this.component = component;
    }
    public void Caller(int value)
    {
        if (value < 2) return;
        if (value > 43) return;

        component.CallThis(value);
    }
}

public interface ITestInterface
{
    void CallThis(int value);
}

[Theory]
[InlineData(1, "Should not call component if less than 2")]
[InlineData(44, "Should not call component if bigger than 43")]
public void Test(int value, string failMsg)
{
    var mock = new Mock<ITestInterface>();
    var testable = new TestClass(mock.Object);

    testable.Caller(value);

    mock.VerifyNoOtherCalls();
}

Back this issue Back this issue

stakx commented 2 years ago

In this (artificial) example, wouldn't it be sufficient to give a more meaningful name to the test method itself (say, Caller_does_not_invoke_component_for_values_not_between_2_and_43)? Your test runner would then output that method name for the test failure. Seems like a simpler solution than having to extend the tooling...

tengl commented 2 years ago

Yeah, it was a bad example. There are more complicated inputs where renaming the test is not as easy. But maybe we should duplicate the test into several facts instead, and give the tests more descriptive names.

stakx commented 1 year ago

@tengl, have you since found a solution that works for you (without us having to extend Moq), or do you still want to follow up on this?

tengl commented 1 year ago

@stakx Sorry for the late reply. I haven't been working in that part of the code for a while. But I guess that breaking the theory into several almost identical facts would solve the issue.

But I still think that a message would be nice to have, but not critical.

github-actions[bot] commented 2 weeks ago

Due to lack of recent activity, this issue has been labeled as 'stale'. It will be closed if no further activity occurs within 30 more days. Any new comment will remove the label.