mstest如何创建在同一对象上工作的测试方法(累积)

本文关键字:对象 工作 累积 测试方法 何创建 创建 mstest | 更新日期: 2023-09-27 18:00:58

我目前正在尝试为我正在构建的计算器实现测试(使用复合模式(。第一个方法应该增加75美元,这很好,但当第二个方法运行时,"服务"将重置,并以0美元作为作业成本。如果我将两种方法合并为一种,那么一切都会如我所期望的那样工作。如何在服务领域保持价值?

[TestClass()]
public class JobTests
{
    private Service service;
    private LaborTime laborTime;
    private LaborRates laborRates;
    [TestInitialize]
    public void init()
    {
        service = new EmergencyService();
    }
    [TestMethod()]
    // add one hour of service at $75/50 rate
    public void Job_OnFullCost_Is75()
    {
        // Arrange
        laborTime = new LaborTime(
            checkIn: new DateTime(year: 2016, month: 7, day: 20, hour: 10, minute: 0, second: 0),
            checkOut: new DateTime(year: 2016, month: 7, day: 20, hour: 11, minute: 0, second: 0)
        );
        laborRates = new LaborRates(75, 50);
        service = new Labor(service, laborTime, laborRates);
        // Act
        var expected = 75.0M;
        var actual = service.JobCost;
        // Assert
        Assert.AreEqual(expected, actual);
    }
    [TestMethod()]
    // add another hour to the service, at same rate of $175/60
    public void Job_OnFullCost_Is125()
    {
        // Arrange
        laborTime = new LaborTime(
            checkIn: new DateTime(year: 2016, month: 7, day: 20, hour: 12, minute: 0, second: 0),
            checkOut: new DateTime(year: 2016, month: 7, day: 20, hour: 13, minute: 0, second: 0)
        );
        LaborRates laborRates = new LaborRates(75, 50);
        service = new Labor(service, laborTime, laborRates);
        //service.IsContinuation = true;
        // Act
        var expected = 125.0M;
        var actual = service.JobCost;
        // Assert
        Assert.AreEqual(expected, actual);
    }
}

mstest如何创建在同一对象上工作的测试方法(累积)

由于单元测试不能保证全部一起运行,也不能保证以特定的顺序运行,因此最好避免在它们之间添加时间依赖性(即要求它们以特定的次序运行才能正常工作(。

相反,将添加人工的"困难部分"提取到一个单独的辅助方法中,并使您的两种方法测试特定的场景。

[TestClass()]
public class JobTests
{
    private Service service;
    [TestInitialize]
    public void init()
    {
        service = new EmergencyService();
    }
    [TestMethod()]
    // add one hour of service at $75/50 rate
    public void Job_OnFullCost_Is75()
    {
        // Arrange
        AddHourOfService(75, 50);
        // Act
        var expected = 75.0M;
        var actual = service.JobCost;
        // Assert
        Assert.AreEqual(expected, actual);
    }
    [TestMethod()]
    // add another hour to the service, at same rate of $175/60
    public void Job_OnFullCost_Is125()
    {
        // Arrange
        AddHourOfService(75, 50);
        AddHourOfService(75, 50);
        // Act
        var expected = 125.0M;
        var actual = service.JobCost;
        // Assert
        Assert.AreEqual(expected, actual);
    }
    private void AddHourOfService(int cost, int time)
    {
        var laborTime = new LaborTime(
        checkIn: new DateTime(year: 2016, month: 7, day: 20, hour: 10, minute: 0, second: 0),
        checkOut: new DateTime(year: 2016, month: 7, day: 20, hour: 11, minute: 0, second: 0)
    );
        var laborRates = new LaborRates(75, 50);
        service = new Labor(service, laborTime, laborRates);
    }
}

除了消除测试之间的时间耦合之外,这还有一个很好的副作用,那就是让测试的目的直接从代码中显而易见。你不再需要像"以75/50美元的价格增加一小时服务"这样的评论,因为AddHourOfService(75, 50)让事情变得非常明显。让代码以这种方式自我文档化是很好的,因为注释很容易与代码不同步(正如你从第二条注释中看到的那样,它写着"175/60美元",而这显然不是测试的目的(。