2

我正在使用 Visual Studio 2010 Ultimate SP1。我的项目中有大约 225 个单元测试,它们都通过了。然而,其中一项测试报告其命中的方法的代码覆盖率为 0%。调试时,我可以单步执行并看到它触及了方法的每一行。然而在代码覆盖率报告中,它显示该方法根本没有被覆盖。所有其他测试和方法在代码覆盖率方面都工作得很好。

我尝试了以下但没有成功:

  • 将正在测试的方法移到另一个类中。
  • 做了方法
  • 静态与非静态。
  • 将测试转移到另一个班级。
  • 删除了我所有的 .testsetting 文件并从头开始重新创建它们
  • 写了一个不同的测试来执行相同的方法,结果相同
  • 重启VS
  • 重新启动

万一重要,该方法位于 Global.asax 文件中。但是,我把它搬到了另一个班级,它没有任何区别。

有任何想法吗?

这是正在测试的方法。

 public void LogError(ILoggingService loggingService, IController controller)
    {
        if (loggingService == null)
            throw new ArgumentNullException("loggingService");

        if (controller == null)
            throw new ArgumentNullException("controller");

        Exception ex = Server.GetLastError();

        loggingService.LogException(ex.Message, ex.StackTrace, ApplicationName.VoucherLog, UserInfo.UserName);


        HttpException httpException = ex as HttpException;

        if (httpException == null)
        {
            var routeData = new RouteData();
            routeData.Values["controller"] = "Error";
            routeData.Values["action"] = "HttpError";

            Server.ClearError();

            var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
            controller.Execute(rc);
        }
    }

引发异常的前 4 行被其他测试命中并显示在代码覆盖率统计信息中。该方法的其余部分受到以下测试的影响(通过调试验证并看到每一行实际上都已执行),但不会显示在代码覆盖率统计信息中。这是测试:

    [TestMethod]
    [HostType("Moles")]
    public void LogError()
    {
        DMVCommon.ApplicationName expectedApplication = DMVCommon.ApplicationName.VoucherLog;
        string expectedUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        NotImplementedException expectedException = null;

        System.Web.Moles.MHttpServerUtility.AllInstances.GetLastError = (System.Web.HttpServerUtility server) =>
        {
            return expectedException;
        };

        System.Web.Moles.MHttpApplication.AllInstances.ContextGet = (System.Web.HttpApplication application) =>
        {
            return MvcMockHelpers.FakeHttpCurrentContext();
        };


        try
        {
            throw new NotImplementedException();
        }
        catch (NotImplementedException exc)
        {
            expectedException = exc;
            using (MvcApplication app = new MvcApplication())
            {
                bool called = false;

                Mock<ILoggingService> mockLoggingService = new Mock<ILoggingService>();
                mockLoggingService.Setup(a => a.LogException(expectedException.Message, expectedException.StackTrace, expectedApplication, expectedUser)).Callback(() => called = true);

                Mock<IController> mockController = new Mock<IController>();

                app.LogError(mockLoggingService.Object, mockController.Object);

                mockController.Verify(a => a.Execute(It.IsAny<System.Web.Routing.RequestContext>()), Times.Exactly(1));
                Assert.IsTrue(called);
            }
        }
    }
4

1 回答 1

0

这可能是因为您使用了Moles - 当运行器加载程序集时,Moles 接管并分析程序集而不是覆盖分析器。

覆盖工具和 Moles存在已知的集成问题- 为了让两个 .Net 分析器(Moles 和覆盖)协同工作,它们必须相互实现特定的支持。

尝试在没有 Moles 的情况下跑步,看看会发生什么......

也可以将其视为类似的示例。

于 2012-05-01T05:06:50.833 回答