Hyper testing among unit and integration testing

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store