I have a web application that sends an sms by consuming an ASMX service(which is now production and working fine), however I now need to unit test this application consumes the asmx service, below is the specific action method I'm interested in unit testing
public ActionResult CreateSms(SendSMSViewModel tblSend)
{
if (ModelState.IsValid)
{
if (_dbManager.SendSMS(tblSend.CellNumber, tblSend.Message, User.Identity.Name))
{
TempData["Success"] = "Message was successfully sent";
}
else
{
TempData["Error"] = "An error occured while sending the message";
}
}
return RedirectToAction("CreateSms");
}
So this action method receives a view model as parameter, this vmodel has 2 properties(cell number, message) and there is also user who is sending the sms. Now in my unit test or integration(apologies I also happen to confuse the two) I have the following code, this first part I am just trying to mock the method(s)
public DatabaseManagerTest()
{
Mock<IDatabaseManager> moqDB = new Mock<IDatabaseManager>();
//Setup send sms test
moqDB.Setup(md => md.SendSMS("0734233173", "Test message", "Ronny.Mahlangu")).Returns(true);
this._mockDatabaseManager = moqDB.Object;
}
and below is the actual test method
public void TestSendSms()
{
bool results = this._mockDatabaseManager.SendSMS("0734233173", "Test message", "Ronny.Mahlangu");
Assert.IsTrue(results);
}
My testing seem to pass, however I also want to unit test the action method itself, in this case I'm only testing whether sms sends successfully, how do I go about testing the action method itself, surely I need to mock the asmx service but I am blank
Also note that the SendSMS
method comes from a class named DatabaseManager
, which is where the asmx methods are being invoked, here is the peace of code from that class
public bool SendSMS(string cellNumber, string message, string logonName) {
if(string.IsNullOrWhiteSpace(cellNumber))
{
throw new ArgumentNullException("cell phone number is null");
}
if(string.IsNullOrWhiteSpace(message))
{
throw new ArgumentNullException("Sms message is null");
}
using (SMSService.SendSmsSoapClient sms = new SMSService.SendSmsSoapClient())
{
if (sms.SendDirect(cellNumber, message, ConfigurationManager.AppSettings["costCentre"], userLogonName, ConfigurationManager.AppSettings["source"]) == SMSService.Status.Successful)
{
SaveSmsDetails(cellNumber, message, userLogonName);
return true;
}
}
return false;
}