gpt4 book ai didi

c# - 如何使用 moq 对在 .NET 中使用 asmx 服务的应用程序进行单元测试

转载 作者:行者123 更新时间:2023-11-30 16:44:19 27 4
gpt4 key购买 nike

我有一个 Web 应用程序,它通过使用 ASMX 服务发送短信(现在可以正常工作了),但是我现在需要对该应用程序使用 asmx 服务进行单元测试,下面是我的具体操作方法对单元测试感兴趣

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");
}

因此此操作方法接收一个 View 模型作为参数,此 vmodel 具有 2 个属性(单元格编号、消息)以及发送短信的用户。现在在我的单元测试或集成中(抱歉我也碰巧混淆了两者)我有以下代码,第一部分我只是想模拟方法

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;
}

下面是实际的测试方法

public void TestSendSms()
{
bool results = this._mockDatabaseManager.SendSMS("0734233173", "Test message", "Ronny.Mahlangu");
Assert.IsTrue(results);
}

我的测试似乎通过了,但是我还想对 Action 方法本身进行单元测试,在这种情况下我只测试短信是否发送成功,我该如何测试 Action 方法本身,当然我需要模拟asmx 服务,但我是空白的

另请注意,SendSMS 方法来自一个名为 DatabaseManager 的类,这是调用 asmx 方法的地方,下面是该类的代码

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;
}

最佳答案

DatabaseManager 是 Controller 的依赖项,它包装了对 asmx 服务的第 3 部分调用。您的第一个测试实际上是在测试模拟,并没有多大用处。要测试 Controller ,您需要为操作提供必要的依赖项才能完成。

这是一个简单的例子。

假设以下界面基于所讨论的原始示例..

public interface IDatabaseManager {
bool SendSMS(string cellNumber, string message, string logonName);
}

下面代表本例中被测 Controller

public class SmsController : Controller {
private IDatabaseManager _dbManager;

public SmsController(IDatabaseManager databaseManager) {
this._dbManager = databaseManager;
}


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");
}
}

测试 Action 需要模拟 Action 中使用的依赖关系。下面展示了如何做到这一点。

[TestMethod]
public void TestCreateSms() {
//Arrange
var expectedCellNumber = "0734233173";
var expectedMessage = "Test message";
var expectedName = "Ronny.Mahlangu";
var expectedResult = true;

// - dependency mocked with desired behavior
var moqDB = new Mock<IDatabaseManager>();
moqDB
.Setup(md => md.SendSMS(expectedCellNumber, expectedMessage, expectedName))
.Returns(expectedResult)
.Verifiable();

// - Fake user for request in order to access User.Identity.Name
var principal = new ClaimsPrincipal(new GenericIdentity(expectedName));
var contextMock = new Mock<HttpContextBase>();
contextMock.Setup(_ => _.User).Returns(principal);

// - controller under test
var controller = new SmsController(moqDB.Object);
controller.ControllerContext = new ControllerContext(contextMock.Object, new RouteData(), controller);

// - view model sent to action
var tblSend = new SendSMSViewModel {
CellNumber = expectedCellNumber,
Message = expectedMessage
};

//Act
var result = controller.CreateSms(tblSend);

//Assert
moqDB.Verify(); //verify that the mock executed as expected
//you can also assert the results of calling the action to confirm expectations
Assert.IsTrue(controller.TempData.ContainsKey("Success"));
Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
var redirectResult = result as RedirectToRouteResult;
Assert.AreEqual("CreateSms", redirectResult.RouteValues["action"]);
}

可以复制和修改上面的代码以测试其他场景,例如当 SendSMS 失败时,只需让模拟的依赖项在调用时返回 false 即可。

这应该足以让您入门。

关于c# - 如何使用 moq 对在 .NET 中使用 asmx 服务的应用程序进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43753435/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com