Real Test Driven Development – Thinking of Money 2

 

The next test we need to write to to create the engine class

        [Test]
        public void Create()
        {
            Assert.IsNotNull(new MoneyEngine.Engine());
        }

 

this test will fail at first, as we don’t have an engine class, so after creating that the test passes and we are off.

next we will need a balance,

 

        [Test]
        public void Balance()
        {
            Assert.AreEqual(0, new MoneyEngine.Engine().Balance);
        }
the test fails so we create the balance code in the engine class
using System;

namespace MoneyEngine
{
    public class Engine
    {
        private double _Balance = 0;
        public double Balance
        {
            get
            {
                return _Balance;
            }
        }
    }
}
the test passes, we need to refractor the test class as we have each test creating the engine. which cant be good,
So I have created a module level variable of type and a setup method, this has the [SetUp] attribute, which
causes NUnit to call this function before running each test, so each test is called with a known starting state.
        MoneyEngine.Engine _Engine;

        [SetUp]
        public void SetupForTest()
        {
            _Engine = new Engine();    
        }

        [Test]
        public void Create()
        {
            Assert.IsNotNull(_Engine);
        }

        [Test]
        public void Balance()
        {
            Assert.AreEqual(0, _Engine.Balance);
        }

        [Test]
        public void Hook()
        {
            Assert.IsTrue(true);
        }
next we need to be able to add to the balance, 
thinking about the future we will need some information about the addition and subtraction, such as a reason for 
the item, a category which we should be able to add to, the date of the event and a unique id. so we are going to
need some sort of collection(s) of items that are additions and subtractions, 
so we are going to need a new test class for the item, and the item class, the item class itself is going to be
just a collection of properties at present so the test cases simply ensure we get out what we have put in. I will
therefore show the completed class and tests in full.
using System;
using NUnit.Framework;

namespace MoneyEngine.UnitTests
{
    [TestFixture]
    public class Tests
    {
        MoneyEngine.MoneyTransaction _MoneyTrans;
        private DateTime _TestDate = DateTime.Now;

        [SetUp]
        public void SetupForTests()
        {
            _MoneyTrans = new MoneyTransaction(10.0, "Category", "Description", _TestDate, TransactionType.AdditionTransaction);
        }

        [Test]
        public void Hook()
        {
            Assert.IsTrue(true);
        }

        [Test]
        public void Create()
        {
            Assert.IsNotNull(_MoneyTrans);
        }

        [Test]
        public void Amount()
        {
            Assert.AreEqual(10.0, _MoneyTrans.TransactionAmount);
        }

        [Test]
        public void Category()
        {
            Assert.AreEqual("Category", _MoneyTrans.TransactionCategory);
        }

        [Test]
        public void Description()
        {
            Assert.AreEqual("Description", _MoneyTrans.TransactionDescription);
        }

        [Test]
        public void TransactionDate()
        {
            Assert.AreEqual(_TestDate, _MoneyTrans.TransactionDate);
        }

        [Test]
        public void TransType()
        {
            Assert.AreEqual(TransactionType.AdditionTransaction, _MoneyTrans.TransType);
        }
    }
}

using System;

namespace MoneyEngine
{
    public enum TransactionType
    {
        AdditionTransaction,
        SubtractionTransaction
    }

    class MoneyTransaction
    {        
        /// <summary>
        /// Initializes a new instance of the MoneyTransaction class.
        /// </summary>
        /// <param name="transactionAmount"></param>
        /// <param name="transactionCategory"></param>
        /// <param name="transactionDescription"></param>
        /// <param name="transactionDate"></param>
        /// <param name="transType"></param>
        public MoneyTransaction(double transactionAmount, string transactionCategory, string transactionDescription, DateTime transactionDate, TransactionType transType)
        {
            _TransactionAmount = transactionAmount;
            _TransactionCategory = transactionCategory;
            _TransactionDescription = transactionDescription;
            _TransactionDate = transactionDate;
            _TransType = transType;
        }
        
        private double _TransactionAmount;
        public double TransactionAmount
        {
            get
            {
                return _TransactionAmount;
            }
        }

        private string _TransactionCategory;
        public string TransactionCategory
        {
            get
            {
                return _TransactionCategory;
            }
        }

        private string _TransactionDescription;
        public string TransactionDescription
        {
            get
            {
                return _TransactionDescription;
            }
        }

        private DateTime _TransactionDate;
        public DateTime TransactionDate
        {
            get
            {
                return _TransactionDate;
            }
        }

        private TransactionType _TransType;
        public TransactionType TransType
        {
            get
            {
                return _TransType;
            }
        }
    }
}

 
 
 

Advertisements

About Duncan Butler

Trying to be a very agile software developer, working in C# with Specflow, Nunit and Machine Specifications, and in the evening having fun with Ruby and Rails
This entry was posted in Projects. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s