Into dotNET
source link: https://intodot.net/verifying-the-execution-order-with-unit-tests/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
3 Oct, 2022
Verifying the execution order with unit tests
Before anything, let’s get something clear: this post is not about the execution order of unit tests, since I strongly recommend you not to do it, as you can read more about here. It’s about verifying the execution order with unit tests of the methods inside a specific method. And why would I want that, you might ask? Well, sometimes you need to do more than just verifying that the correct methods are called. For example, let’s suppose you are implementing the BreakfastRoutine method of your Human class. It would be really good to be sure that he would always brush his teeth after eating, right? I for one believe that my implementation of this method fails sometimes.
How to do it
In order to do that, we can take advantage of Moq’s Callback feature. Simply put, it’s something that allows us to execute a piece of code right after a method has been called. With it, we can set up a flag to keep track of when a method has been invoked, and then consume that flag to verify if the second method has been called at the right time. Continuing with our breakfast example, let’s see how it would like in the code:
Here’s our Human.cs:
public class Human
{
public IHygieneFunctions HygieneFunctions { get; }
public IMouthStuff MouthStuff { get; }
public Human(IHygieneFunctions hygieneFunctions, IMouthStuff mouthStuff)
{
HygieneFunctions = hygieneFunctions;
MouthStuff = mouthStuff;
}
public void BreakfastRoutine()
{
MouthStuff.ChewYummyYummies();
HygieneFunctions.BrushTeeth();
}
}
Simple, right? And here it would be our test:
[Test]
public void BreakfastRoutine_ShouldBrushTeethAfterEating()
{
//Arrange
var hygieneFunctionsMock = new Mock<IHygieneFunctions>();
var mouthStuffMock = new Mock<IMouthStuff>();
var human = new Human(hygieneFunctionsMock.Object, mouthStuffMock.Object);
var hasChewed = false;
var teethBrushedAfterChewing = false;
mouthStuffMock.Setup(x => x.ChewYummyYummies()).Callback(() => hasChewed = true);
hygieneFunctionsMock.Setup(x => x.BrushTeeth()).Callback(() => teethBrushedAfterChewing = hasChewed);
//Act
human.BreakfastRoutine();
//Assert
Assert.That(teethBrushedAfterChewing, Is.True);
}
So, what’s going on is:
- We create our mocks and our Human
- We set a callback for the ChewYummyYummies method, so when it’s called, it will set the hasChewed flag to true
- We also set a callback for the BrushTeeth method, so it will only be true if the hasChewed variable is true, which allows us to verify what we want
Conclusion
Pretty simple right? With just some lines of logic, we are verifying the execution order with unit tests, saving our beloved human of having to brush twice and possibly missing his train. Even better, we didn’t even have to modify our code to reach our objective, keeping our solutions as pure as possible.
3 Oct, 2022
Leave a Reply Cancel reply
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK