Reset call tracking

4 posts, 0 answers
  1. Jerrie Pelser
    Jerrie Pelser avatar
    4 posts
    Member since:
    Jan 2005

    Posted 27 May 2013 Link to this post

    Hi,

    I have a unit test where I assert that a certain action will call a method on a mock dependency object by using Mock.Assert().  I want to ensure that the action I take calls the method on the mock object exactly once.  The problem is that the setup of the unit test creates a scenario where the method of the mock object will also be called, so when I assert that the call to the mock object happened just once it fails because it has actually been called more than once.

    Is there a way to "reset" the call tracking of methods on mock objects?  I basically want to tell JustMock that at a certain point, whatever calls have happened to my mock objects should be discarded and the call counter should basically start at 0 again.

    Am I missing something in the framework or is there another way I should do this?

    Jerrie
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 28 May 2013 Link to this post

    Hi Jerrie,

    Thank you for the detailed explanation.

    I was able to reproduce the issue on my side.

    Unfortunately, there is no such functionality in JustMock at this point. However, the idea sounds great, so I have added it to our Ideas and Feedback portal. To vote for its faster implementation check this link.

    Nevertheless, I also managed to find a workaround for this. Let`s assume we have the following:
    [TestClass]
    public class JustMockTest1
    {
        public MyClass myMock;
     
        [TestInitialize]
        public void TestInit()
        {
            myMock = Mock.Create<MyClass>(Behavior.CallOriginal);
     
            Mock.Arrange(() => myMock.DoSmt()).DoNothing();
     
            myMock.Foo();
        }
     
        [TestMethod]
        public void ShouldCheckForAllDoSmt()
        {
            myMock.Foo();
     
            Mock.Assert(() => myMock.DoSmt(), Occurs.Once());
        }
    }
    This will fail as the DoSmt() has actually been called two times (I believe you are experiencing similar issue).
    However, to avoid this, I wrote the test like this:
    [TestMethod]
    public void ShouldCheckForTheArrangedDoSmt()
    {
        Mock.Arrange(() => myMock.DoSmt()).DoNothing().OccursOnce();
         
        myMock.Foo();
     
        Mock.Assert(myMock);
    }
    This test passes. So, the workaround is to use occurrence expectations in the arrange.

    I hope this helps.

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  3. DevCraft R3 2016 release webinar banner
  4. Jerrie Pelser
    Jerrie Pelser avatar
    4 posts
    Member since:
    Jan 2005

    Posted 28 May 2013 Link to this post

    That works great!  

    Thank you for the assistance.
  5. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 29 May 2013 Link to this post

    Hi Jerrie,

    I am happy to see this did the job.

    Please, contact us again if there is anything else.

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top