Help me understand mocking occurrence to test void methods?

3 posts, 1 answers
  1. geoff
    geoff avatar
    4 posts
    Member since:
    Jun 2017

    Posted 16 Aug 2018 Link to this post

    I'm trying to use JustMock to test void methods in my class, seeing if underlying conditions are triggered by trying to asserting occurrence on underlying calls. I can't seem to find a good example of this. Here is some quick code to show what I mean.

    Example Class:

        public class MockingOccurrance
        {
            public void MyVoidMethod(string myString)
            {
                switch (myString)
                {
                    case "goodstring":
                        GoodString(myString);
                        break;
                    default:
                        BadString(myString);
                        break;
                }
            }

            public void BadString(string badString) =>  Console.WriteLine($"{badString} is a bad string");
            public void GoodString(string goodString) => Console.WriteLine($"{goodString} is a good string");
        }

     

    Example Test:

           [Test]
            public void TestMethod1()
            {
                var mo = new MockingOccurrance();
                mo.MyVoidMethod("a bad string");

                Mock.Assert(() => mo.BadString(Arg.AnyString), Occurs.Once());
            }

    Obviously this doesn't work. Is there any way to make the test detect the call of BadString(string badString) without alot of smelly interfaces?

    Thanks!
    -Geoff

  2. Answer
    Mihail
    Admin
    Mihail avatar
    257 posts

    Posted 17 Aug 2018 Link to this post

    Hello Geoff,

    You are correct, JustMock won't count the execution of BadString in your test. This is because JustMock doesn't know that it should count it. To solve this, you should create the instance of MockingOccurrance with Mock.Create<MockingOccurrance>(). However, the default overload of the Create method will create the mock with RecursiveLose behavior and the call to the MyVoidMethod method will be stubbed. For this problem, you can take two approaches. The first one is to use the Create method overload that takes the Behaviour.CallOriginal parameter. This will force all methods of the class MockingOccurrance to execute the original implementation Like this:
    var mo = Mock.Create<MockingOccurrance>(Behavior.CallOriginal);
    mo.MyVoidMethod("a bad string");
     
    Mock.Assert(() => mo.BadString(Arg.AnyString), Occurs.Once());

    The second approach is to arrange the MyVoidMethod method. In this case, only the MyVoidMethod will call the original implementation and the rest of the methods will be stubbed. Like this:
    var mo = Mock.Create<MockingOccurrance>();
    Mock.Arrange(() => mo.MyVoidMethod(Arg.AnyString)).CallOriginal();
     
    mo.MyVoidMethod("a bad string");
     
    Mock.Assert(() => mo.BadString(Arg.AnyString), Occurs.Once());
    It depends on your scenario and your preference which approach you will take.

    I hope this information answers your questions.

    Regards,
    Mihail
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. geoff
    geoff avatar
    4 posts
    Member since:
    Jun 2017

    Posted 17 Aug 2018 in reply to Mihail Link to this post

    Fantastic answer! Thank you very much!
Back to Top