Real Test Driven Development – Thinking of Money 13

 

OK so I have tested the binding, and made sure that what I thought might work did, transferring this to our formal code means we need a way to test it as close to the interface as possible, personally I believe for programmer unit testing it is impossible to test actually at the interface short of actually clicking on the buttons.

My current way, and I am always open to suggestions, is to use a model view controller pattern, the model in this case is the engine, the view is the form and the controller will communicate between them both, and it is here that my testing will stop, so I want zero code if possible in the form, and all the code that is usually in the form within the controller.

so to the unit tests which look like this.

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

        [Test]
        public void Create()
        {
            Assert.IsNotNull(new ThinkingOfMoney.Controller.DataGridController());
        }

        [Test]
        public void BindDataGrid()
        {
            System.Windows.Forms.DataGridView gridView = new System.Windows.Forms.DataGridView();

            DataGridController controller = new DataGridController();
            controller.BindDataGrid(gridView);

            Assert.IsTrue(gridView.Rows.Count == 0);
        }
    }

This is our usual hook and a create, the BindDataGrid is where the interest will happen it takes our forms grid , the test then ensures we have some rows when the function returns.

The controller code looks like this

    class DataGridController
    {
        MoneyEngine.MoneyEngine _Engine;

        public DataGridController()
        {
            _Engine = new MoneyEngine.MoneyEngine();                      
        }

        public void BindDataGrid(System.Windows.Forms.DataGridView gridView)
        {
            _Engine.LoadTransactions();

            gridView.DataSource = _Engine.History;
        }
    }

The controller creates a new money engine in its constructor, providing the link to the model, so the form dosn’t actually have any direct contact with the money engine at all.

The BindDataGrid method takes a data grid view as a parameter, and loads all the transactions, then the data grid view data source property is set to the engine history collection.

Finally we have to create the loading code for the main form

    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();

            Controller.DataGridController gridController = new ThinkingOfMoney.Controller.DataGridController();
            gridController.BindDataGrid(dgvHistory);
        }
    }

This is the bit we cant test, so it has to be as basic as possible, now we can run the project and see how the user interface fills out.

so next we need to display the balance, and be able to add and update the transactions

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