DL *_*han 6 c# asp.net-mvc unit-testing mstest
[HttpPost]
[Route("TnC")]
public IHttpActionResult TnC(CustomViewModel myViewModel)
{
try
{
return Json(_Internal.TnC(myViewModel, LoggedInUser));
}
catch (BusinessException exception)
{
return Json(BuildErrorModelBase(exception));
}
}
Run Code Online (Sandbox Code Playgroud)
如果_Internal服务具有99.99%的正常运行时间且未定义正式的故障合同接口.
在我的应用程序级别(业务层级别)中作为BusinessException- 根类处理的异常
其中BusinessException的定义如下
public class BusinessException : Exception
{
BusinessException()...
BusinessExceptionFoo()...
BusinessExceptionBar()...
//...
}
Run Code Online (Sandbox Code Playgroud)
而目前的测试方法是
要做的事:添加异常测试
[TestMethod]
[ExpectedException(typeof(BusinessException),
"Not a valid Business Case")]
public void TnCTest()
{
var bookingService = myContainer.Resolve<mySvc>();
var controller = new LinkBookingController(mySvc, myContainer);
var expectedResult = controller.TnC(new myViewModel
{
...params
});
var actualResult = GetData<Result<myViewModel>>(expectedResult);
Assert.AreEqual(expectedResult, actualResult);
}
Run Code Online (Sandbox Code Playgroud)
expectedResult==actualResult不测试代码的异常块.除了手动删除以太网电缆以获取此特定类型的服务器错误之外,如何构造使服务抛出异常的请求.
我能想到的最好的是
#if DEBUG && UnitTestExceptions
throw new BusinessException();
#endif
Run Code Online (Sandbox Code Playgroud)
但是有一个更好的选择.
所测试的方法有一些值得关注的事情。
它将横切关注点混合在应该重构为ExceptionHandler. 很可能该代码段在该控制器和其他类似控制器中重复多次(DRY)。
public class WebApiExceptionHandler : ExceptionHandler {
public override void Handle(ExceptionHandlerContext context) {
var innerException = context.ExceptionContext.Exception;
// Ignore HTTP errors
if (innerException.GetType().IsAssignableFrom(typeof(System.Web.HttpException))) {
return;
}
if(innerException is BusinessException) {
context.Result = BuildErrorResult(exception);
return;
}
//...other handler code
}
IHttpActionResult BuildErrorResult(BusinessException exception) {
//... your logic here
}
}
Run Code Online (Sandbox Code Playgroud)
可以使用以下扩展方法将处理程序添加到HttpConfiguration启动期间,同时还假设应用程序正在利用依赖关系反转服务。
public static HttpConfiguration ReplaceExceptionHandler(this HttpConfiguration config) {
var errorHandler = config.Services.GetExceptionHandler();
if (!(errorHandler is WebApiExceptionHandler)) {
var service = config.Services.GetService(typeof(WebApiExceptionHandler));
config.Services.Replace(typeof(IExceptionHandler), service);
}
return config;
}
Run Code Online (Sandbox Code Playgroud)
现在,跨领域关注点已经得到处理,操作变得更加简单且更容易测试。这是 ApiController 的简化示例
public class LinkBookingController : ApiController {
private IBookingService bookingService;
public LinkBookingController(IBookingService service) {
bookingService = service;
}
[HttpPost]
[Route("TnC")]
public IHttpActionResult TnC(CustomViewModel myViewModel) {
return Json(bookingService.TnC(myViewModel, User));
}
}
Run Code Online (Sandbox Code Playgroud)
其中IBookingService定义为
public interface IBookingService {
BookingModel TnC(CustomViewModel viewModel, IPrincipal user);
}
Run Code Online (Sandbox Code Playgroud)
使用像 Moq 这样的模拟框架,可以根据需要抛出异常。
[TestMethod]
[ExpectedException(typeof(BusinessException), "Not a valid Business Case")]
public void TnC_Should_Throw_BusinessException() {
//Arrange
var bookingService = new Mock<IBookingService>();
var controller = new LinkBookingController(bookingService.Object);
var viewModel = new myViewModel
{
//...params
};
bookingService.Setup(_ => _.TnC(viewModel, It.IsAny<IPrincipal>())).Throws<BusinessException>()
//Act
var expectedResult = controller.TnC(viewModel);
//Assert
//...the ExpectedException attribute should assert if it was thrown
}
Run Code Online (Sandbox Code Playgroud)
要测试如何处理该异常,请对异常处理程序而不是控制器进行单元测试,因为这不是控制器的责任。
尝试保持控制器精简并专注于其 UI 问题。
| 归档时间: |
|
| 查看次数: |
257 次 |
| 最近记录: |