gpt4 book ai didi

c# - mstest 如何创建对同一对象(累积)起作用的测试方法?

转载 作者:行者123 更新时间:2023-11-30 20:34:46 27 4
gpt4 key购买 nike

我目前正在尝试对我正在构建的计算器(使用复合模式)进行测试。第一种方法应该添加 75 美元,效果很好,但是当第二种方法运行时,“服务”被重置并且有0 美元作为工作成本。如果我将这两种方法合二为一,那么一切都会按照我的预期进行。如何在服务领域保持值(value)?

[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);
}
}

最佳答案

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

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

[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) 很明显会发生这种情况。让代码以这种方式 self 记录是件好事,因为注释很容易与您的代码不同步(正如您从第二条注释中看到的那样,上面写着“$175/60”,而这显然不是测试所做的)。

关于c# - mstest 如何创建对同一对象(累积)起作用的测试方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38776518/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com