Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this test name just a bit over the top

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


    }
like image 506
Sekhat Avatar asked Nov 10 '10 12:11

Sekhat


People also ask

Why it is called Over the Top?

What is the definition of OTT? OTT stands for “Over The Top” and refers to any streaming service that delivers content over the internet. The service is delivered “over the top” of another platform, hence the moniker.

What is OTT stand for?

OTT (over-the-top) is a means of providing television and film content over the internet at the request and to suit the requirements of the individual consumer. The term itself stands for “over-the-top”, which implies that a content provider is going over the top of existing internet services.

What is another term for Over the Top video?

The term is most synonymous with subscription-based video-on-demand (SVoD) services that offer access to film and television content (including existing series acquired from other producers, as well as original content produced specifically for the service).

What is an example of OTT?

What are some examples of OTT? There's a wide range of OTT platforms, including Netflix, Disney+, Hulu, Amazon Prime Video, Hulu, Peacock, CuriosityStream, Pluto TV, and so many more.


2 Answers

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.

like image 194
Adam Ralph Avatar answered Sep 18 '22 21:09

Adam Ralph


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.

like image 43
Gerrie Schenck Avatar answered Sep 21 '22 21:09

Gerrie Schenck