Search code examples
c#unit-testingmicrosoft-fakespex

How to unit test an abstract class using Microsft Fakes framework


I have the following abstract class for which I want to write a unit test. I am new to Microsoft Fakes and so far I have only used it for testing public classes.

public abstract class ProvideBase 
{
    private string tag = string.Empty;

    public string Tag
    {
        get { return tag; }         
        set { tag = value; }
    }

}

public static String GetMyConfig(string sectionName)
{
    MyConfiguration config = MyConfiguration.GetConfig(sectionName);
    return config.GetMyConfig(config.DefaultConfig);
}

I wrote a unit test for my GetMyConfig() method. My test coverage is not 100% however, since I have not used the Tag property. Is there a way I can test it to?

Pex does some kind of mocking to test such things. How do I mock/test the Tag property using Microsoft Fakes?


Solution

  • I'm not really sure why you want to use Fakes for this. Deriving a class from it makes it easy enough to test:

    class TestableProvideBase : ProvideBase{}
    
    [TestMethod]
    public void TestTagProperty() {
        var sut = new TestableProvideBase();
    
        Assert.AreEqual(String.Empty, sut.Tag);
    
        sut.Tag = "someValue";
    
        Assert.AreEqual("someValue", sut.Tag);
    }