Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / C#

Visual Studio Unit Testing Extensions

Rate me:
Please Sign up or sign in to vote.
2.75/5 (4 votes)
23 Dec 2007CPOL6 min read 21.1K   70   10  
An article on creating extensions for unit testing.
using System.ComponentModel;
using CodeProject.VisualStudio.QualityTools.UnitTestFramework;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Test.UnitTestFramework
{
    [TestClass()]
    public class PropertyChangedWatcherTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        private class TestObject : INotifyPropertyChanged
        {
            bool value;
            public bool Value
            {
                get { return value; }
                set
                {
                    if (this.value != value)
                    {
                        this.value = value;
                        OnPropertyChanged("Value");
                    }
                }
            }

            private void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }

            #region INotifyPropertyChanged Members

            public event PropertyChangedEventHandler PropertyChanged;

            #endregion
        }

        [TestMethod()]
        public void TestIfShouldHaveSeenSucceedsWhenPropertyChanged()
        {
            TestObject target = new TestObject();
            PropertyChangedWatcher watcher = new PropertyChangedWatcher(target);
            target.Value = true;
            Specify.ThatAction(
                delegate
                {
                    Specify.That(watcher).ShouldHaveSeen("Value");
                }).ShouldNotThrow();
        }

        [TestMethod()]
        public void TestIfShouldHaveSeenFailsWhenPropertyNotChanged()
        {
            TestObject target = new TestObject();
            PropertyChangedWatcher watcher = new PropertyChangedWatcher(target);
            Specify.ThatAction(
                delegate
                {
                    Specify.That(watcher).ShouldHaveSeen("Value");
                }).ShouldThrow<AssertFailedException>();
        }

        [TestMethod()]
        public void TestIfShouldHaveSeenFailsAfterReset()
        {
            TestObject target = new TestObject();
            PropertyChangedWatcher watcher = new PropertyChangedWatcher(target);
            target.Value = true;
            watcher.Reset();
            Specify.ThatAction(
                delegate
                {
                    Specify.That(watcher).ShouldHaveSeen("Value");
                }).ShouldThrow<AssertFailedException>();
        }
    }
}

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
Windows developer with 10+ years experience working in the banking industry.

Comments and Discussions