Strucuring unit tests

    public class AddressValidator
    {
        public bool IsValid(IList<string> addressLines)
        {
            if (addressLines == null)
            {
                throw new ArgumentNullException("addressLines",
                                                "AddressLines cannot be null");
            }

            if (addressLines.ElementAtOrDefault(0) != null && 
                addressLines.ElementAtOrDefault(4) != null)
            {
                return true;
            }

            if (addressLines.ElementAtOrDefault(0) != null &&
                addressLines.ElementAtOrDefault(1) != null &&
                addressLines.ElementAtOrDefault(2) != null)
            {
                return true;
            }

            return false;
        }

        public bool IsNonUkAddress(IEnumerable<string> addressLines)
        {
            // return true for UK address
            
            return false;
        }
        
    }
    [TestClass]
    public class AddressVaidatorTests
    {

        private AddressValidator _target;

        [TestInitialize]
        public virtual void TestSetup()
        {
            _target = new AddressValidator();
            // other test setups that are common for all the tests in this file.
        }

        [TestClass]
        public class TheIsValidMethod : AddressVaidatorTests
        {

            [TestInitialize]
            public override void TestSetup()
            {
                base.TestSetup();
                // additional test setups applicable for the current method under test
            }

            [TestClass]
            public class WhenTheAddressIsNull : TheIsValidMethod
            {

                // add test initialiser here to setup anything that are 
                // applicable for the current scenario

                [TestMethod]
                [ExpectedException(typeof(ArgumentNullException))]
                public void ShouldThrowArgumentNullException()
                {
                    // arrange
                    List<string> inputAddress = null;

                    // act
                    _target.IsValid(inputAddress);

                    // assert
                    // expected exception - ArgumentNullException.

                }

                // add test cleanups here

            }

            [TestClass]
            public class WhenAddressLine1AndPostcodeArePresent : TheIsValidMethod
            {

                // add test initialiser here to setup anything that are 
                // applicable for the current scenario

                [TestMethod]
                public void ShouldReturnTrue()
                {
                    // arrange
                    var inputAddress = new List<string>()
                    {
                        "Address1", null, null, null, "Postcode"
                    };

                    // act
                    var actual = _target.IsValid(inputAddress);

                    // assert
                    Assert.IsTrue(actual);

                }

                // add test cleanups here

            }

            // add test cleanups here

        }

        [TestClass]
        public class TheIsUkAddressMethod : AddressValidator
        {
            // tests
        }

        [TestCleanup]
        public virtual void TestCleanup()
        {
            // any cleanups
        }

    }
comments powered by Disqus