I suggest you ...

please add a "Times" argument to IVerifies.Verifiable

I would like to be able to specify the number of times a method should be called from a "Setup" so that if I need to setup multiple methods on a mock I can use a single Verify()

For example, instead of:
mock.Setup(x=>x.Foo()).Returns(1);
mock.Setup(x=>x.Bar()).Returns(2);
//exercise interface
mock.Verify(x=>x.Foo(), Times.Once(), "Foo should be called once");
mock.Verify(x=>x.Bar(), Times.Once(), "Bar should be called once");

I would prefer:
mock.Setup(x=>x.Foo()).Returns(1).Verifiable(Times.Once(), "Foo should be called once");
mock.Setup(x=>x.Bar()).Returns(2).Verifiable(Times.Once(), "Bar should be called once");
//exercise interface
mock.Verify();

Thank you for your consideration.

23 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    QuinnQuinn shared this idea  ·   ·  Admin →

    4 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Marco KijlstraMarco Kijlstra commented  · 

        If you could refer to the Arranged setup somehow, like

        mock.Setup(x => x.Foo()).Returns(1).Id("s1");

        Then you could avoid duplicating the setup expression without violating the AAA principle:

        mock.Verify("s1").Times.Once();

        Or maybe like this:

        var verifier = mock.Setup(x => x.Foo()).Returns(1).Verifier();
        ...
        verifier.Times.Once()

      • Dmitry SarkisyanDmitry Sarkisyan commented  · 

        I need this also! Without this option I have to duplicate the expression in Setup and Verify phases. Instead of specifying Times param in Setup, as suggested, it could be done during the Verify stage like mock.Verify(Times.Once).

      • Brandyn PhelpsBrandyn Phelps commented  · 

        I gave this all of my votes because, otherwise, I'm having to manually track expression instances to do this without having to declare the same expression twice.

      • Daniel CazzulinoAdminDaniel Cazzulino (Admin, moq) commented  · 

        People have been moving away from Verifiable/Verify without parameters, since it's not very AAA.

        I'd be interested to know what others think too...

      Feedback and Knowledge Base