我同意 Osherove 的观点,即单元测试应该有一个断言,而不是很多。但是,当我将该主体应用于 ASP.NET MVC 单元测试时,我想知道它是否过于复杂。考虑这些测试:
    [TestMethod] 
    public void RedirectTest() {     
        // Arrange - create the controller   
        ExampleController target = new ExampleController();     
        // Act - call the action method     
        RedirectResult result = target.Redirect();   
        // Assert - check the result
        Assert.IsFalse(result.Permanent);     
        Assert.AreEqual("/Example/Index", result.Url); 
    }
    [TestMethod]
    public void RedirectValueTest() {     
        // Arrange - create the controller     
        ExampleController target = new ExampleController();     
        // Act - call the action method     
        RedirectToRouteResult result = target.Redirect();     
        // Assert - check the result     
        Assert.IsFalse(result.Permanent);     
        Assert.AreEqual("Example", result.RouteValues["controller"]);    
        Assert.AreEqual("Index", result.RouteValues["action"]);    
        Assert.AreEqual("MyID", result.RouteValues["ID"]); 
    }
当然,上面的两个测试可以分成六个测试(每个断言一个),但感觉有点过分了。有没有围绕这个的最佳实践?你做什么工作?如果这是测试专家共识的所在,我肯定会沿着每个断言进行一次测试的路径...