This is a migrated thread and some comments may be shown as answers.

Reset call tracking

3 Answers 47 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jerrie Pelser
Top achievements
Rank 1
Jerrie Pelser asked on 28 May 2013, 04:13 AM
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

3 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 28 May 2013, 10:56 AM
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.
0
Jerrie Pelser
Top achievements
Rank 1
answered on 29 May 2013, 01:42 AM
That works great!  

Thank you for the assistance.
0
Kaloyan
Telerik team
answered on 29 May 2013, 07:01 AM
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.
Tags
General Discussions
Asked by
Jerrie Pelser
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Jerrie Pelser
Top achievements
Rank 1
Share this question
or