C# 由于设计不当,单元测试有时会失败
下面是两个类Sensor和Indicator,其中包含一些业务逻辑和一个单元测试。单元测试有时因设计不当而失败。我试图修复实现并更正单元测试,但仍然遇到问题。我需要你的帮助来修复实现C# 由于设计不当,单元测试有时会失败,c#,unit-testing,mstest,C#,Unit Testing,Mstest,下面是两个类Sensor和Indicator,其中包含一些业务逻辑和一个单元测试。单元测试有时因设计不当而失败。我试图修复实现并更正单元测试,但仍然遇到问题。我需要你的帮助来修复实现 public class Sensor { const double Offset = 16; public double GetFuelAmountValue() { double value; SampleAmount(out value);
public class Sensor
{
const double Offset = 16;
public double GetFuelAmountValue()
{
double value;
SampleAmount(out value);
return Offset + value;
}
private static void SampleAmount(out double fuelTelemetryValue)
{
Random basicRandomNumbersGenerator = new Random();
fuelTelemetryValue = basicRandomNumbersGenerator.Next(0, 25);
}
}
public class Indicator
{
private const double LowFuelTreshold = 7;
private const double HighFuelTreshold = 21;
Sensor _sensor = new Sensor();
bool _alarm = false;
private long _alarmCount = 0;
public void Check()
{
double LitersOfFuelValue = _sensor.GetFuelAmountValue();
if (LitersOfFuelValue < LowFuelTreshold || HighFuelTreshold < LitersOfFuelValue)
{
_alarm = true;
_alarmCount += 1;
}
}
public bool Alarm
{
get { return _alarm; }
}
}
我知道这是一个简化的代码,而不是生产代码。这里有一种方法可以模拟随机生成的数字
FuelTelementryNumber
,并通过构造函数在测试中通过
public class Sensor
{
const double Offset = 16;
Func<double> _getFuelTelemetry;
public Sensor(Func<double> getFuelTelemetry)
{
_getFuelTelemetry = getFuelTelemetry;
}
public Sensor()
: this(SampleAmount)
{
}
public double GetFuelAmountValue()
{
double value = _getFuelTelemetry();
return Offset + value;
}
private static double SampleAmount()
{
Random basicRandomNumbersGenerator = new Random();
return basicRandomNumbersGenerator.Next(0, 25);
}
}
public class Indicator
{
private const double LowFuelTreshold = 7;
private const double HighFuelTreshold = 21;
Sensor _sensor;
public Indicator(Sensor sensor)
{
_sensor = sensor;
}
public Indicator() : this(new Sensor())
{
}
bool _alarm = false;
private long _alarmCount = 0;
public void Check()
{
double LitersOfFuelValue = _sensor.GetFuelAmountValue();
if (LitersOfFuelValue < LowFuelTreshold || HighFuelTreshold < LitersOfFuelValue)
{
_alarm = true;
_alarmCount += 1;
}
}
public bool Alarm
{
get { return _alarm; }
}
}
[TestClass]
public class FuelIndicatorTest
{
[TestMethod]
public void Foo()
{
Indicator indicator = new Indicator(new Sensor(() => {return 25;}));
indicator.Check();
Assert.AreEqual(true, indicator.Alarm);
}
}
公共类传感器
{
常数双偏移=16;
Func_getFuelTelemetry;
公共传感器(Func getFuelTelemetry)
{
_getFuelTelemetry=getFuelTelemetry;
}
公共传感器()
:此(样本量)
{
}
公共双GetFuelAmountValue()
{
双值=_getFuelTelemetry();
返回偏移量+值;
}
专用静态双采样量()
{
Random basicRandomNumbersGenerator=新随机();
返回basicRandomNumbersGenerator.Next(0,25);
}
}
公共类指标
{
私有常量双低FueltReshold=7;
私有常数双高FuelTresHold=21;
传感器(u)传感器;;
公共指示器(传感器)
{
_传感器=传感器;
}
公共指示器():此(新传感器())
{
}
布尔报警=假;
私有长_alarmCount=0;
公共作废检查()
{
double LitersOfFuelValue=_sensor.GetFuelAmountValue();
if(litersofuelvalue{return 25;}));
指示器。检查();
Assert.AreEqual(true,indicator.Alarm);
}
}
为什么您总是假定报警为假?随机数生成可能会产生一个结果,以将原因指示器。报警设置为真。您需要以某种方式模拟随机生成的数字FuelTelementryNumber,并在测试中通过,以使测试可预测1。您现在可以摆脱SampleAmount()
,对吗?2.如果你模拟燃料遥测逻辑总是返回25,超过HighFuelTreshold
(原文如此),你不应该测试报警状态是true
,而不是false
?以去除SampleAmount()-我想这取决于作者。据我所知,问题是如何改进设计以正确测试。这里有一个例子说明如何做到这一点。
public class Sensor
{
const double Offset = 16;
Func<double> _getFuelTelemetry;
public Sensor(Func<double> getFuelTelemetry)
{
_getFuelTelemetry = getFuelTelemetry;
}
public Sensor()
: this(SampleAmount)
{
}
public double GetFuelAmountValue()
{
double value = _getFuelTelemetry();
return Offset + value;
}
private static double SampleAmount()
{
Random basicRandomNumbersGenerator = new Random();
return basicRandomNumbersGenerator.Next(0, 25);
}
}
public class Indicator
{
private const double LowFuelTreshold = 7;
private const double HighFuelTreshold = 21;
Sensor _sensor;
public Indicator(Sensor sensor)
{
_sensor = sensor;
}
public Indicator() : this(new Sensor())
{
}
bool _alarm = false;
private long _alarmCount = 0;
public void Check()
{
double LitersOfFuelValue = _sensor.GetFuelAmountValue();
if (LitersOfFuelValue < LowFuelTreshold || HighFuelTreshold < LitersOfFuelValue)
{
_alarm = true;
_alarmCount += 1;
}
}
public bool Alarm
{
get { return _alarm; }
}
}
[TestClass]
public class FuelIndicatorTest
{
[TestMethod]
public void Foo()
{
Indicator indicator = new Indicator(new Sensor(() => {return 25;}));
indicator.Check();
Assert.AreEqual(true, indicator.Alarm);
}
}