所有这些观点甚至都没有标题。所以我不能做一个
Assert.AreEqual( “错误”,Viewbag.Title);
我还能如何测试错误控制器以确保涵盖至少 85%代码
public class ErrorController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult NotFound()
{
return View();
}
public ActionResult BadRequest()
{
return View();
}
public ActionResult ServerError()
{
return View();
}
}
我能想到的最好的是
public class ErrorControllerTests : BaseTestController
{
ErrorController ec = new ErrorController();
[TestMethod()]
public void IndexTest()
{
var actionResult = ec.Index() as ViewResult;
Assert.AreSame(ec.ViewData, actionResult.ViewData);
Assert.AreSame(ec.TempData, actionResult.TempData);
Assert.AreEqual(actionResult.ViewName,"");
}
[TestMethod()]
public void NotFoundTest()
{
var NotFoundTestResult = ec.NotFound() as ViewResult;
Assert.AreEqual(NotFoundTestResult.ViewName,"");
}
[TestMethod()]
public void BadRequestTest()
{
var badRequestTestResult = ec.BadRequest() as ViewResult;
Assert.AreEqual(badRequestTestResult.ViewName,"");
}
[TestMethod()]
public void ServerErrorTest()
{
var serverErrorTestResult = ec.ServerError() as ViewResult;
Assert.AreEqual(serverErrorTestResult.ViewName, "");
}
}
答案 0 :(得分:1)
您可以测试示例视图名称:
Assert.AreEqual("Index", result.ViewName);
但要做到这一点,您需要在操作返回中指定视图名称:
public class ErrorController : Controller
{
public ActionResult Index()
{
return View("Index");
}
public ActionResult NotFound()
{
return View("NotFound");
}
public ActionResult BadRequest()
{
return View("BadRequest");
}
public ActionResult ServerError()
{
return View("ServerError");
}
}
答案 1 :(得分:1)
您基本上尝试测试应该由拥有该框架的Microsoft完成的框架。您应该尽量避免测试您不拥有或控制的代码。
也就是说,这是一个测试ErrorController
的工作,试图模仿框架功能并解决单元测试的局限性。
[TestClass]
public class ErrorControllerTests {
[TestMethod]
public void TestIndexView() {
//Arrange
var expectedViewName = GetViewNameFromExpression<ErrorController>(c => c.Index());
var controller = ArrangeErrorController(expectedViewName);
//Act
var result = controller.Index() as ViewResult;
//Assert
Assert.IsNotNull(result);
//Replicating Framework functionality
MockExecuteResult(controller, result);
Assert.AreEqual(expectedViewName, result.ViewName);
CollectionAssert.AreEquivalent(controller.ViewData.ToList(), result.ViewData.ToList());
CollectionAssert.AreEquivalent(controller.TempData.ToList(), result.TempData.ToList());
}
private static void MockExecuteResult(ErrorController controller, ViewResult result) {
try {
result.View = Mock.Of<IView>();
result.ExecuteResult(controller.ControllerContext);
} catch { }
}
private static ErrorController ArrangeErrorController(string actionName) {
var controllerName = "error";
var context = GetControllerContext(actionName, controllerName);
var controller = new ErrorController() {
ControllerContext = context
};
context.Controller = controller;
return controller;
}
private static ControllerContext GetControllerContext(string actionName, string controllerName) {
RouteData rd = new RouteData();
rd.Values["action"] = actionName;
rd.Values["controller"] = controllerName;
var mockHttpContext = new Mock<HttpContextBase>();
mockHttpContext.Setup(c => c.Session).Returns((HttpSessionStateBase)null);
mockHttpContext.Setup(_ => _.Response.Output).Returns(new StringWriter());
return new ControllerContext(mockHttpContext.Object, rd, new Mock<Controller>().Object);
}
private static string GetViewNameFromExpression<TController>(Expression<Action<TController>> action) {
if (action == null) {
throw new ArgumentNullException("action");
}
var type = typeof(TController);
bool isController = type != null
&& type.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)
&& !type.IsAbstract
&& typeof(IController).IsAssignableFrom(type);
if (!isController) {
throw new InvalidOperationException("Invalid controller.");
}
MethodCallExpression body = action.Body as MethodCallExpression;
if (body == null)
throw new InvalidOperationException("Expression must be a method call.");
if (body.Object != action.Parameters[0])
throw new InvalidOperationException("Method call must target lambda argument.");
string actionName = body.Method.Name;
return actionName;
}
}