-1

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 does? If so how do we instantiate the MyConfiguration inside ShimMyConfiguration ?

  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.

krrishna
  • 2,050
  • 4
  • 47
  • 101

1 Answers1

1

As it stands, your test is pretty pointless. I could satisfy the test with the following code:

public object Create(object parent, object configContext, XmlNode section)
{
    return new int();
}

If this is one of many tests for the Create method then this isn't the end of the world, as long as the other tests cover the rest of the desired behaviour.

Based on the method you've posted, I would want to write at least the following tests:

  1. Validate that if the supplied section is null, ArgumentNullException is thrown.
  2. Validate that the section supplied to your Create call is passed to an instance of MyConfiguration and that the same instance is returned from the Create call.

Assuming that test 1 exists somewhere else, test 2 could look something like this:

[TestMethod]        
public void TestMyConfigurationUsedToReadConfiguration()
{            
    MyConfiguration calledOnInstance = null;     
    MyConfiguration returnedInstance = null;
    XmlNode calledWithSection = null;
    XmlNode sectionIn = null;

    using (ShimsContext.Create())
    {
        ShimMyConfiguration.Constructor = @this => new ShimMyConfiguration()
        {
            return;
        };
        ShimMyConfiguration.AllInstances.LoadValuesFromConfigXmlNode = (a,b) =>
        {
            calledOnInstance = a;
            calledWithSection = b;
            return;
        };
        var _MyConfigurationHandler = new MyConfigurationHandler();
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml("<Common.Mys><Mys><add name=\"LoggingErrorMessage\"/><remove name=\"LoggingMessageBox\"/></Mys></Common.Mys>");          
        sectionIn = xmlDoc.SelectSingleNode("Common.Mys");
        returnedInstance = _MyConfigurationHandler.Create(null, null, sectionIn);                 

    }           
    Assert.IsNotNull(returnedInstance);
    Assert.AreEqual(returnedInstance, calledOnInstance);
    Assert.AreEqual(sectionIn, calledWithSection);
}
forsvarir
  • 10,749
  • 6
  • 46
  • 77
  • That's helpful. And would you not worry about the Constructor test ? Or what I have shimmed is good enough ? Its about my first question. – krrishna May 28 '15 at 06:57
  • @krrishna I don't see what there is to test around the constructor. You don't pass anything into it. The important bit of functionality is that a configuration is told to load values from an xml section and that the same instance of configuration is returned. I would expect you to have tests (possibly integration tests) somewhere else that actually test the configuration class works correctly and these tests might care about its constructor, depending on what it does. – forsvarir May 28 '15 at 08:14