正如标题所暗示的,这个测试名称是不是只是一个小标题?
WhenChargeIsGreaterThanRestingChargeButLessThanChargeRestApproachStep_OnUpdate_ChargeIsSetToRestingCharge对如何改进这一点有什么建议吗?或者它就像现在这样好吗?
下面是完整的测试夹具,这样您就可以获得一些上下文:)
public class NeuronTests
{
[Fact]
public void OnUpdate_NeuronFiresWhenChargeIsEqualToThreshold()
{
Neuron neuron = new Neuron();
bool fired = false;
neuron.Fired += (s, e) => fired = true;
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
Assert.True(fired, "Neuron didn't fire");
}
[Fact]
public void OnUpdate_NeuronDoesntFireWhenChargeIsLessThanThreshold()
{
Neuron neuron = new Neuron();
bool fired = false;
neuron.Fired += (s, e) => fired = true;
neuron.Charge = Neuron.ChargeThreshold - 1f;
neuron.Update();
Assert.False(fired, "Neuron fired!");
}
[Fact]
public void OnUpdate_NeuronFiresWhenChargeIsGreaterThanThreshold()
{
Neuron neuron = new Neuron();
bool fired = false;
neuron.Fired += (s, e) => fired = true;
neuron.Charge = Neuron.ChargeThreshold + 1f;
neuron.Update();
Assert.True(fired, "Neuron didn't fire");
}
[Fact]
public void WhenNeuronFires_ChargeResetsToRestingCharge()
{
Neuron neuron = new Neuron();
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
Assert.Equal(Neuron.RestingCharge, neuron.Charge);
}
[Fact]
public void AfterFiring_OnUpdate_NeuronWontFire()
{
Neuron neuron = new Neuron();
int fireCount = 0;
neuron.Fired += (s, e) => fireCount++;
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
Assert.Equal(1, fireCount);
}
[Fact]
public void WhenResting_OnUpdate_NeuronWillFire()
{
Neuron neuron = new Neuron();
int fireCount = 0;
neuron.Fired += (s, e) => fireCount++;
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
neuron.Charge = Neuron.ChargeThreshold;
neuron.Update();
Assert.Equal(2, fireCount);
}
[Fact]
public void WhenChargeIsGreaterThanRestingCharge_OnUpdate_ChargeDecreasesTowardsRestingCharge()
{
Neuron neuron = new Neuron();
neuron.Charge = Neuron.RestingCharge + (2 * Neuron.ChargeRestApproachStep);
neuron.Update();
Assert.Equal(Neuron.RestingCharge + Neuron.ChargeRestApproachStep, neuron.Charge);
}
[Fact]
public void WhenChargeIsGreaterThanRestingChargeButLessThanChargeRestApproachStep_OnUpdate_ChargeIsSetToRestingCharge()
{
Neuron neuron = new Neuron();
neuron.Charge = Neuron.RestingCharge + (Neuron.ChargeRestApproachStep * 0.5f);
neuron.Update();
Assert.Equal(Neuron.RestingCharge, neuron.Charge);
}
}发布于 2010-11-10 21:01:58
我个人的观点是,方法名称永远不会太长,只要它们是描述性的。
单元测试名称往往要长得多,因为它们必须包含更多信息。这对我来说也没问题,因为它们只出现在方法签名和您的测试列表中(这是您想要有一个好名字的地方),您永远不会从任何其他代码中调用它们。
发布于 2010-11-10 21:13:02
布局此类测试的一种流行方法是使用嵌套类和典型的BDD实践建议的给定/When/Then类型词汇表,例如
public class NeuronStory
{
public class GivenChargeIsGreaterThanRestingCharge
{
public class GivenChargeIsLessThanChargeRestApproachStep
{
public class WhenUpdated
{
public void ThenChargeIsSetToRestingCharge()
{
}
}
}
}
}通过这种方式,您还可以在相同的位置嵌套其他测试,这些测试也适合GivenChargeIsGreaterThanRestingCharge故事情节。
发布于 2010-11-10 23:05:28
下划线给出了您认为应该从方法名中移出的线索。
然后你可以:
public class NeuronOnUpdateTests
{
public void WhenChargeIsBetweenRestingChargeAndChargeRestApproachStep
{
//Charge is set to resting state
Assert.True(x);
}
}https://stackoverflow.com/questions/4144616
复制相似问题