Last active
February 9, 2022 06:03
-
-
Save tocalai/8d22517051bc79aba0134e281e3614f3 to your computer and use it in GitHub Desktop.
Moq sample for testing MonitorService.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestClass] | |
public class MonitorServiceTest | |
{ | |
private MonitorService _monitorService; // System under test (SUT) | |
private Mock<MailService> _mockMailService; // Test Double | |
private Mock<IOhterService> _mockOtherService; // Test Double | |
private IOptions<ConfigModel> _configModelOptions; // Configuration | |
[TestInitialize] // Setup | |
public void Initialize() | |
{ | |
_configModelOptions = Options.Create(new ConfigModel | |
{ | |
APIBaseInfo = new APIBaseInfo | |
{ | |
MailReceiverUri = "/v1/mail/service/receiver/{roles}", | |
} | |
}); | |
_mockMailService = new Mock<MailService>(MockBehavior.Loose, new object[] { _configModelOptions, null}){}; | |
_mockOtherService = new Mock<IOtherService>(MockBehavior.Loose); | |
_monitorService = new MonitorService(_configModelOptions, _mockMailService.Object, _mockOtherService.Object); | |
} | |
[TestMethod] | |
public void Test_System_Tracking_Heart_Beat_Send_Mail_To_Group1_Should_Return_Sucesss() | |
{ | |
// arrange | |
var role = "Group1"; | |
var heartbeat = "{\"SystemName\":\"PaymentAPI\", \"Status\":\"Health\"}"; | |
// override method that we just want to print out the information but not real drop to mail server | |
_mockMailService.Setup(m => m.SendMailNotify(It.IsAny<List<Receiver>>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string[]>(), It.IsAny<string>())).Callback((IEnumerable<Receiver> receiverList, string subject, string content, string[] filePaths, string tag) => | |
{ | |
// write to debug output windows | |
Debug.WriteLine($"{subject}, {content}."); | |
}); | |
// we will retrieve the real role(s) data back that help us for saving preparing diverse mock data in the testing target | |
_mockMailService.Setup(m => m.GetReceivers(role)).CallBase(); | |
// execute | |
var result = _monitorService_monitorService.TrackHeartBeat(heartbeat); | |
// verify | |
Assert.AreEqual("Success", result.Status); | |
} | |
[TestMethod] | |
public void Test_System_Tracking_Heart_Beat_Down_Send_Mail_To_Admin_Should_Return_Sucesss() | |
{ | |
// arrange | |
var role = "Admin"; | |
var heartbeat = "{\"SystemName\":\"PaymentAPI\", \"Status\":\"Down\"}"; | |
// override method that we just want to print out the information but not real drop to mail server | |
_mockMailService.Setup(m => m.SendMailNotify(It.IsAny<List<Receiver>>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string[]>(), It.IsAny<string>())).Callback((IEnumerable<Receiver> receiverList, string subject, string content, string[] filePaths, string tag) => | |
{ | |
// write to debug output windows | |
Debug.WriteLine($"{subject}, {content}."); | |
}); | |
// we will retrieve the real role(s) data back that help us for saving preparing diverse mock data in the testing target | |
_mockMailService.Setup(m => m.GetReceivers(role)).CallBase(); | |
// execute | |
var result = _monitorService_monitorService.TrackHeartBeatDown(heartbeat); | |
// verify | |
Assert.AreEqual("Success", result.Status); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment