Click here to Skip to main content
15,896,726 members
Articles / Web Development / ASP.NET

Designing Application Using Test Driven Development

Rate me:
Please Sign up or sign in to vote.
4.49/5 (18 votes)
1 May 2008CPOL4 min read 39.4K   206   60  
In this article, we will take a look at designing application using test driven development.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MbUnit.Framework;
using DomainObjects;
using DomainObjects.CustomExceptions;

namespace TestSuite
{
    [TestFixture]
    public class when_saving_a_person
    {

        [Test]
        [ExpectedException(typeof(InvalidAgeException))]
        public void should_throw_invalid_age_exception_if_age_is_invalid()
        {
            Person person = new Person() 
            { FirstName = "Mohammad", LastName = "Azam", DOB = DateTime.Parse("01/02/2000") }; 

        }


        [Test]
        [RollBack]        
        public void should_be_able_to_update_person_successfully()
        {
            Person person = new Person() { FirstName = "Mohammad", LastName = "Azam", DOB = DateTime.Parse("02/09/1981") };
            person.Save();

            Person vPerson = Person.GetById(person.Id);
            vPerson.FirstName = "Mohammad1";
            vPerson.LastName = "Azam1";
            vPerson.DOB = DateTime.Parse("02/10/1981");

            Console.WriteLine(vPerson.DOB);

            vPerson.Save();

            Person uPerson = Person.GetById(vPerson.Id);
            Assert.AreEqual(vPerson.FirstName, uPerson.FirstName,"First name not updated!");
            Assert.AreEqual(vPerson.LastName, uPerson.LastName,"Last name not updated!");
            Assert.AreEqual(vPerson.DOB, uPerson.DOB,"dob is not being updated!"); 
        }

        [Test]
        [RollBack]
        public void should_make_sure_multiple_addresses_are_saved_when_person_is_created()
        {
            Person person = new Person() { FirstName = "Mohammad", LastName = "Azam", DOB = DateTime.Parse("02/09/1981") };

            IList<Address> addresses = new List<Address>();
            addresses.Add(new Address(person) { Street = "Richmond" });
            addresses.Add(new Address(person) { Street = "Harvin" });
            addresses.Add(new Address(person) { Street = "Fondren" });

            ((List<Address>) addresses).ForEach(delegate(Address a) 
            {
                person.AddAddress(a); 
            });

            person.Save();

            Person vPerson = Person.GetById(person.Id);
            
            // verify the addresses
            for (int i = 0; i < addresses.Count; i++)
            {
                Assert.AreEqual(addresses[i].Street, vPerson.Addresses[i].Street);
            }

        }

        [Test]
        [RollBack]
        public void should_make_sure_address_is_saved_when_person_is_saved()
        {
            Person person = new Person() { FirstName = "Mohammad", LastName = "Azam", DOB= DateTime.Parse("02/09/1981") };
            person.AddAddress(new Address(person) { Street = "Richmond" });

            person.Save();

            Assert.IsTrue(person.Addresses[0].Id > 0); 

            // get the person object with the address! 

            Person vPerson = Person.GetById(person.Id);
            Console.WriteLine(vPerson.Addresses.Count); 

            Assert.AreEqual(person.Addresses[0].Street, vPerson.Addresses[0].Street);
            
        }

        [Test]
        public void should_be_able_to_add_an_address_to_the_person()
        {
            Person person = new Person() { FirstName = "Mohammad", LastName = "Azam" };

            Address address = new Address(person);
            person.AddAddress(address);

            Assert.AreEqual(1, person.Addresses.Count()); 
        }

        [Test]
        public void should_make_sure_that_an_address_must_have_a_person()
        {
            Address address = new Address(new Person());
            Assert.IsNotNull(address.Person);
        }

        // just a simple method to test that if the age received is correct or not! 
        [RowTest]
        [Row("02/09/1981",27)]
        [Row("02/09/1999",9)]
        [Row("07/17/1982",25)]
        public void can_get_the_correct_age(DateTime dob, int actualAge)
        {           
            int yearsOld = (DateTime.Now.Subtract(dob).Days / 365);
            Assert.AreEqual(actualAge, yearsOld);
        }       
        
        [RowTest]
        [Row("","","02/09/2000", ExpectedException = typeof(InvalidAgeException))]       
        public void should_not_set_person_properties_if_in_invalid_state(string firstname, string lastname,DateTime dob)
        {
            Person person = new Person() { FirstName= firstname, LastName = lastname, DOB = dob };
            Assert.IsTrue(person.FirstName != firstname,"FirstName has been set!");
            Assert.IsTrue(person.LastName != lastname,"LastName has been set!");
            Assert.IsTrue(person.DOB != dob,"DOB has been set!");          
        }              

        [Test]
        [RollBack]
        public void should_check_if_the_person_saved_successfully()
        {
            Person person = new Person() { FirstName = "Mohammad", LastName = "Azam", DOB= DateTime.Parse("02/09/1981") };

            person.Save();

            Assert.IsTrue(person.Id > 0);

            Person vPerson = Person.GetById(person.Id);
            Assert.AreEqual(person.FirstName, vPerson.FirstName);
            Assert.AreEqual(person.LastName, vPerson.LastName);
            Assert.AreNotEqual(person.DOB, vPerson.DOB);
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
My name is Mohammad Azam and I have been developing iOS applications since 2010. I have worked as a lead mobile developer for VALIC, AIG, Schlumberger, Baker Hughes, Blinds.com and The Home Depot. I have also published tons of my own apps to the App Store and even got featured by Apple for my app, Vegetable Tree. I highly recommend that you check out my portfolio. At present I am working as a lead instructor at DigitalCrafts.




I also have a lot of Udemy courses which you can check out at the following link:
Mohammad Azam Udemy Courses

Comments and Discussions