MS测试单元测试框架不会破坏其构造函数

时间:2020-11-10 06:59:47

标签: c# mstest

我的MSTest测试类确实包含用于初始化日志记录功能特定变量的构造函数。 在调试模式下运行单元测试方法时,发现我的调试器没有命中我的构造函数。由于我们可以在MSTest中使用[TestInitialize]方法来强制该方法在执行任何方法之前执行,因此我为此创建了Startup()方法。即使是现在,调试器也没有使用这种方法。因此,我所有的测试方法都失败了。

任何人都可以指出我做错的地方!有关解决此问题的任何建议,请...

[TestClass]
public class AutomaticInformationUpdateTest
{
    private readonly ILogger<AutomaticInformationUpdate> _logger;
    private static ILoggerFactory _loggerFactory;
    private readonly IServiceProvider _serviceProvider;

    //ISSUE: When executing individual methods, or complete test class, this constructor is not calling
    public AutomaticInformationUpdateTest(ILoggerFactory loggerFactory, IServiceProvider serviceProvider)
    {
        _loggerFactory = loggerFactory;
        _serviceProvider = serviceProvider;
        _logger = _loggerFactory.CreateLogger<AutomaticInformationUpdate>();
    }

    // ISSUE: I should not use this method as I cannot initialize most of the private variables.
    // Even then, I tried this method with the hope that the debugger can can this method.
    // Debugger is not hitting even for this method.
    [TestInitialize]
    public void Startup()
    {
        _logger = _loggerFactory.CreateLogger<AutomaticInformationUpdate>();
    }

    [TestMethod]
    public void ProcessFullEngineInformation_Test()
    {
        IAutomaticInformationUpdate updater = new AutomaticInformationUpdate(_loggerFactory, _serviceProvider);
        Assert.IsTrue(updater.ProcessFullInformation().GetAwaiter().GetResult()); 
    }

    [TestMethod]
    public void ProcessIncrimentalEngineInformation_Test()
    {
        IAutomaticInformationUpdate updater = new AutomaticInformationUpdate(_loggerFactory, _serviceProvider);
        Assert.IsTrue(updater.ProcessIncrimentalInformation().GetAwaiter().GetResult());
    }

    [TestMethod]
    public void ProcessLvlFullInformation_Test()
    {
        IAutomaticInformationUpdate updater = new AutomaticInformationUpdate(_loggerFactory, _serviceProvider);
        Assert.IsTrue(updater.ProcessLvlFull().GetAwaiter().GetResult());
    }

    [TestMethod]
    public void ProcessLvlIncrimentalInformation_Test()
    {
        IAutomaticInformationUpdate updater = new AutomaticInformationUpdate(_loggerFactory, _serviceProvider);
        Assert.IsTrue(updater.ProcessLvlIncrimental().GetAwaiter().GetResult());
    }
}

1 个答案:

答案 0 :(得分:0)

您是否尝试过将其关闭然后重新打开? caching the VS IDE does的数量会让您感到惊讶。虽然我不主张删除单个缓存的文件,但我将尝试重新启动IDE,清理解决方案,然后重试。您的代码对我来说很好。

您可能想通过在测试方法中设置断点来确认是否遇到了断点。这会告诉您调试设置是否损坏。