Retail Planning Part 3

Published on Sunday, November 12, 2017

Code Available

The source for this project is available now on GitHub

Testing Unit

We are going to use TDD/BDD for testing this project as we are building it. Here are some of the basic tests for the PlanVariable class. As you can see from the unit test class below, I have decided to use xUnit.

    public class PlanVariableTests
    {
        [Fact]
        public void LockedVariableValueDoesNotChange()
        {
            var variable = new PlanVariable(string.Empty, 100, true);
            var attempt = variable.Update(200);

            Assert.Same(variable, attempt);
        }

        [Fact]
        public void UnlockedVariableValueDoesChange()
        {
            const decimal newValue = 200;
            var variable = new PlanVariable(string.Empty, 100, false);
            var attempt = variable.Update(newValue);

            Assert.Equal(newValue, attempt.Value);
            Assert.NotSame(variable, attempt);
        }

        [Fact]
        public void StartWithUnlockAndThenLock()
        {
            const decimal newValue = 200;
            var variable = new PlanVariable(string.Empty, 100, false);
            var locked = variable.Lock();
            var attempt = locked.Update(newValue);

            Assert.Equal(variable.Value, locked.Value);
            Assert.Same(locked, attempt);
        }

        [Fact]
        public void StartWithLockAndThenUnlock()
        {
            const decimal newValue = 200;
            var variable = new PlanVariable(string.Empty, 100, true);
            var unlocked = variable.Unlock();
            var attempt = unlocked.Update(newValue);

            Assert.Equal(newValue, attempt.Value);
            Assert.NotSame(unlocked, attempt);
        }
    }

Acceptance Testing

For acceptance testing, we are going to use StoryTeller. For now until we get more code, the tests look similar to the unit tests.

    public class PlanVariableFixture : Fixture
    {
        public PlanVariableFixture()
        {
            Title = "Plan Variable Specification";
        }

        private PlanVariable variable;

        [FormatAs("Starting with a locked variable with {startValue}")]
        public void LockedPlanVariable(decimal startValue)
        {
            variable = new PlanVariable("spec", startValue, true);
        }

        [FormatAs("Starting with a unlocked variable with {startValue}")]
        public void UnlockedPlanVariable(decimal startValue)
        {
            variable = new PlanVariable("spec", startValue, false);
        }

        [FormatAs("Change the value {value}")]
        public void ChangeTheValue(decimal value)
        {
            variable = variable.Update(value);
        }

        [FormatAs("The value should be {value}")]
        public decimal TheValueShouldBe()
        {
            return variable.Value;
        }
    }

The results of the acceptance tests:

StoryTellerPlanVariable

comments powered by Disqus