Alex Rivera | Logout

Is this test name just a bit over the top

Asked 2010-11-10T12:47:28.500
12

As the title suggests, is this test name just a little of the top?

WhenChargeIsGreaterThanRestingChargeButLessThanChargeRestApproachStep_OnUpdate_ChargeIsSetToRestingCharge

Any suggestions on how to improve this? or is it fine as it is?

Below is the whole test fixture as it stands so you can get some context :)

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;
            ne
Edit
Report

2 Answers

20

One popular way to layout tests like these is to use nested classes with a Given/When/Then type vocabulary as suggested by typical BDD practices, e.g.

public class NeuronStory
{
    public class GivenChargeIsGreaterThanRestingCharge
    {
        public class GivenChargeIsLessThanChargeRestApproachStep
        {
            public class WhenUpdated
            {
                public void ThenChargeIsSetToRestingCharge()
                {
                }
            }
        }
    }
}

This way you can also nest other tests which also fit into the GivenChargeIsGreaterThanRestingCharge storyline in the same place.

answered 2010-11-10T13:13:01.957
15

My personal opinion is that method names can never be too long, as long as they are descriptive.

Unit test names tend to be a lot longer, since they have to contain more information. This is fine for me too, since they only appear in the method signature and in your list of tests (and this is where you want to have a good name), you'll never call them from any other code.

answered 2010-11-10T13:01:57.557

Your Answer