Hyper testing among unit and integration testing

Using framework Moq as example

LAI TOCA
2 min readFeb 9, 2022

--

Photo from: http://xunitpatterns.com/Test%20Stub.html

We have a story that talked about how to mock the object(s)/service(s) [Test Double] for separating depended-on component (DOC) from data source (database…) , third-party’s API…etc. Then we could focus on target function(s) what we need to verify.

But sometimes we would having hyper testing scenario that our target testing function(s) might interactivities with diverse source(s) or dependency, and also hard to prepare test fixture for different test case(s).

Suppose we have a system under test (SUT): MonitorService. The service will monitoring systems that depended-on component (DOC): MailService and OtherService.

// MointorService.cs
public class MonitorService
{
private readonly IMailService _mailService;
private readonly IOtherService _otherService;
public MonitorService(ConfigModel config, IMailService mailService, IOtherService otherService) {//...}public ResultModel TrackHeartBeat(string heartbeatInfo)
{
var retModel = new ResultModel {Status = "Success"};
// get receiver
var receivers = _mailService.GetReceivers("Group1");

// logic on heartbeat info
// _otherService.method1();

// send mail to relative receiver
_mailService.SendMailNotify(...);
// if everything goes as expected return completed signal
return retModel;
} public ResultModel TrackHeartBeatDown(string heartbeatInfo)
{
var retModel = new ResultModel {Status = "Success"};
// get receiver
var receivers = _mailService.GetReceivers("Admin");

// logic on heartbeat info
// _otherService.method1();
// logic on receivers
// for example send direct sms for the system owner
// _otherService.method2(owner, ...);

// send mail to relative receiver
_mailService.SendMailNotify(...);
// if everything goes as expected return completed signal
return retModel;
}}

Write down the test case for verifying methods: TrackHeartBeat & TrackHeartBeatDown. So far we will relied on the receivers role inside the database (through call API), so that we could saving time to stub receivers information over each test cases.

Make sure to let your methods: SendMailNotify/GetReceivers as virtual that enable Moq to override the specific behaviors.

Happy testing and enjoying coding😼

Reference

--

--

LAI TOCA

Coding for fun. (Either you are running for food or running for being food.)