这个测试名称是否略高于顶部

Sek*_*hat 12 c# tdd unit-testing naming-conventions xunit.net

正如标题所示,这个测试名称只是顶部的一点吗?

WhenChargeIsGreaterThanRestingChargeButLessThanChargeRestApproachStep_OnUpdate_ChargeIsSetToRestingCharge
Run Code Online (Sandbox Code Playgroud)

关于如何改进这个的任何建议?或者它是好的吗?

下面是整个测试夹具,因此您可以获得一些上下文:)

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


    }
Run Code Online (Sandbox Code Playgroud)

Ada*_*lph 20

像这样布局测试的一种流行方法是使用具有Given/When/Then类型词汇表的嵌套类,如典型的BDD实践所建议的,例如

public class NeuronStory
{
    public class GivenChargeIsGreaterThanRestingCharge
    {
        public class GivenChargeIsLessThanChargeRestApproachStep
        {
            public class WhenUpdated
            {
                public void ThenChargeIsSetToRestingCharge()
                {
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这样你也可以GivenChargeIsGreaterThanRestingCharge在同一个地方嵌套其他适合故事情节的测试.


Ger*_*nck 15

我个人认为方法名称永远不会太长,只要它们是描述性的.

单元测试名称往往更长,因为它们必须包含更多信息.这对我来说也很好,因为它们只出现在方法签名和测试列表中(这是你想要一个好名字的地方),你永远不会从任何其他代码中调用它们.