views:

170

answers:

4

I have been thinking a lot about unit testing and how to improve the readability of the unit tests. I thought why not give a character to the classes in the unit test to clarify what they do.

Here is a simple unit test that I wrote:

[TestFixture]
    public class when_dave_transfers_money_from_wamu_account_to_the_woodforest_account 
    {
        [Test]
        public void should_increase_the_amount_in_woodforest_account_when_transaction_successfull()
        {
            Dave dave = new Dave();

            Wamu wamu = new Wamu();
            wamu.Balance = 150; 

            wamu.AddUser(dave);

            Woodforest woodforest = new Woodforest();
            woodforest.AddUser(dave);

            FundTransferService.Transfer(100, wamu, woodforest);

            Assert.AreEqual(wamu.Balance, 50); 
            Assert.AreEqual(woodforest.Balance, 100); 
        }
}

Here is the Dave class:

/// <summary>
    /// This is Dave!
    /// </summary>
    public class Dave : User
    {      
        public Dave()
        {
            FirstName = "Dave";
            LastName = "Allen";            
        }

    }

The unit test name clearly serves the purpose. But, maybe I want to dig a little deeper and assign the Wamu and Woodforest accounts to Dave whenever Dave is created. The problem is that it will move away from readability as I will have to use index values to refer to the account.

What are your thoughts on making this more readable?

+1  A: 

How about a private helper method in the test fixture

private Dave GetDave_With_Wamu_And_Woodforest_AccountsHookedUp()
Gishu
A: 

I can add Dave to the Wamu and the Woodforest account when Dave is created like this:

 public Dave()
        {
            FirstName = "Dave";
            LastName = "Allen"; 

            // add accounts for Dave 

            Wamu wamu = new Wamu();
            wamu.AddUser(this);

            Woodforest woodforest = new Woodforest();
            woodforest.AddUser(this); 
        }

The accounts are added the List collection in the User object from which Dave inherits.

azamsharp
Wouldn't that be too much in the constructor?
phjr
Yeah that was what I was thinking!
azamsharp
A: 

when you attempt to instantiate the Wamu instance, shouldn't it throw a WamuNotFoundException?

Darren Kopp
What!! not sure what you meant! but my question is different.
azamsharp
because washington mutual is no longer around anymore ;)
Darren Kopp
ahhh! now that is a good one! :)
azamsharp
+1  A: 

Here is another way to run the test:

 [Test]
        public void should_increase_the_amount_in_woodforest_account_when_transaction_successfull()
        {
            Dave dave = new Dave();

            // we know that dave has wamu and wooforest accounts 

            dave.WamuAccount("Wamu").Balance = 150;

            FundTransferService.Transfer(100, dave.WamuAccount("Wamu"), dave.WoodforestAccount(
                "Woodforest"));

            Assert.AreEqual(50, dave.WamuAccount("Wamu").Balance);
            Assert.AreEqual(100, dave.WoodforestAccount("Woodforest").Balance);             
        }
azamsharp