Click here to Skip to main content
15,884,986 members
Articles / DevOps / Testing

Mocking Methods and Properties Using PostSharp

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
14 Aug 2012CPOL4 min read 18.9K   97   7  
Mock test without interface, on static methods and properties, using PostSharp
using System;
using Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestCases
{
    [TestClass]
    public class Tests
    {
        [TestInitialize]
        public void ClearApparatus()
        {
            TestApparatus.Reset();
        }

        [TestCleanup]
        public void CleanupApparatus()
        {
            TestApparatus.VerifyAccess();
        }

        [TestMethod]
        [Description("The test will fail upon calling the static method")]
        [ExpectedException(typeof(NotImplementedException))]
        public void FailedOnStaticProvider()
        {
            SampleObject sample = new SampleObject();
            sample.Methods();
        }

        [TestMethod]
        [Description("With only static method mocked, the test will still fail")]
        [ExpectedException(typeof(NotImplementedException))]
        public void FailedOnInstancedProvider()
        {
            // Mock the static method
            TestApparatus.MockStatic<bool, int>(StaticProvider.Provide).If(input => true).Return(true);

            SampleObject sample = new SampleObject();
            sample.Methods();
        }

        [TestMethod]
        [Description("With both methods mocked, the test should pass")]
        public void AllSuccess()
        {
            // Mock both the static method and instance method
            TestApparatus.MockStatic<bool, int>(StaticProvider.Provide).Any().Return(true);
            TestApparatus.MockInstance<InstancedProvider, bool, int>(i => i.Provide).Expecting(1).If(a1 => a1 >= 0).Return(true);

            SampleObject sample = new SampleObject();
            sample.Methods();
        }

        [TestMethod]
        [Description("Without property mocking")]
        public void WithoutMockingProperty()
        {
            SampleObject sample = new SampleObject();

            Assert.AreEqual(SampleObject.TestingValue, sample.Values());
        }

        [TestMethod]
        [Description("Mocking a specific property")]
        public void MockingProperty()
        {
            const int mocked = 12034;

            TestApparatus.MockProperty(() => StaticProvider.Value).Any().Return(mocked);
            TestApparatus.MockProperty((InstancedProvider i) => i.Value).Expecting(1).Any().Return(mocked);

            SampleObject sample = new SampleObject();
            Assert.AreEqual(mocked, sample.Values());
        }

        [TestMethod]
        [Description("Mocking a property with some condition")]
        public void MockingPropertyConditionally()
        {
            int threshold = 0;

            TestApparatus.MockProperty((InstancedProvider i) => i.Value).If(i => threshold > 1000).Return(5);

            SampleObject sample = new SampleObject();

            threshold = 10;
            Assert.AreEqual(SampleObject.TestingValue, sample.Values());

            threshold = 10000;
            Assert.AreEqual(5, sample.Values());
        }
    }
}

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
Software Developer (Senior) 3PLearning
Australia Australia
Lead Developer, MMO Game Company
Testor, Microsoft

Comments and Discussions