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

Help me understand mocking occurrence to test void methods?

2 Answers 302 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
geoff
Top achievements
Rank 1
geoff asked on 16 Aug 2018, 07:33 PM

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 Answers, 1 is accepted

Sort by
0
Accepted
Mihail
Telerik team
answered on 17 Aug 2018, 11:45 AM
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
0
geoff
Top achievements
Rank 1
answered on 17 Aug 2018, 04:59 PM
Fantastic answer! Thank you very much!
Tags
General Discussions
Asked by
geoff
Top achievements
Rank 1
Answers by
Mihail
Telerik team
geoff
Top achievements
Rank 1
Share this question
or