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

--

--

--

Coding for fun

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why OCR Fails? 5 Limitations Of Conventional OCR Engines

Use Fuse to Inject Failure to I/O

Stack Traces

Importing CSV file into Django Database

TOP MOBILE APPLICATIONS THAT CAN MAKE YOUR KIDS LIKE PROGRAMMING.

Swift PH #25: Combine API, Log in (Best Practices), & iOS Developer Career Growth

MQTT: Emergence of pub/sub model

List of common tools for a web developer

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
LAI TOCA

LAI TOCA

Coding for fun

More from Medium

[Dot Net Core](Graphic series )3.How to actually implement DI Resolve Service

Stripe Payment Element in ASP.NET & C# - Accept One-Time & Recurring Payments

Docker, tools for uniting developers

Strategy pattern — an inevitable strategy