dimanche 24 mai 2015

valid use of Shim in a Test Method in VS 2013 for c# code

I have the below test method code :

[TestMethod]        
public void TestWithNotNull()
{            
    using (ShimsContext.Create())
    {
        ShimMyConfiguration.Constructor = @this => new ShimMyConfiguration()
        {
            return;
        };
        ShimMyConfiguration.AllInstances.LoadValuesFromConfigXmlNode = (a,b) =>
        {
            return;
        };
        var _MyConfigurationHandler = new MyConfigurationHandler();
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml("<Common.Mys><Mys><add name=\"LoggingErrorMessage\"/><remove name=\"LoggingMessageBox\"/></Mys></Common.Mys>");          
        var config = _MyConfigurationHandler.Create(null, null, xmlDoc.SelectSingleNode("Common.Mys"));                        
        Assert.IsNotNull(config);
    }           
}

Actual Code/Method i am testing:

public object Create(object parent, object configContext, XmlNode section)
{
    if (section == (XmlNode)null)
        throw new ArgumentNullException("section");

    MyConfiguration config = new MyConfiguration();
    config.LoadValuesFromConfigXml(section);
    return config;
}

The way i unit tested the above create method is correct ? Because i have below questions:

  1. I have shimmed the Constructor but didnt do what actual constructor MyConfiguration would have done. And is it mandatory to do what actual constructor ?

  2. I have shimmed the LoadValuesFromConfigXmlNode but didn't care of what config object of the statement config.LoadValuesFromConfigXml(section) contains when it is returned .

  3. Because of the points 1 & 2, i am just tesing if config is not null or not. But is it enough to verify like this or do i really need to test against the xml content that has been passed in the call _MyConfigurationHandler.Create(null, null, xmlDoc.SelectSingleNode("Common.Mys")) with what is returned ?

Any answers would help me evaluate if I am making best use of shims or not.

Aucun commentaire:

Enregistrer un commentaire