4

I have a method base.ResolveDate() inside my test method that's coming from a base class and its public and virtual. I want to stub/shim this method with my own, so do I stub or shim? Stub or Shim, how would I go about doing it? From my experience with MS Fakes it seems like it would be a Stub because Stub can only influence overridable methods. - ALM 2012

Here is the test method:

public override DateTime ResolveDate(ISeries comparisonSeries, DateTime targetDate)
    {
        if (comparisonSeries == null)
        {
            throw new ArgumentNullException("comparisonSeries");
        }

        switch (comparisonSeries.Key)
        {               
            case SeriesKey.SomeKey1:
            case SeriesKey.SomeKey2:
            case SeriesKey.SomeKey3:
            case SeriesKey.SomeKey4:
            case SeriesKey.SomeKey5:
                return DateHelper.PreviousOrCurrentQuarterEnd(targetDate);
        }

        return base.ResolveDate(comparisonSeries, targetDate);
    }

Here is the method from the base class I want to Stub/Shim?

public virtual DateTime ResolveDate(ISeries comparisonSeries, DateTime targetDate)
    {            
        if (this.key == comparisonSeries.Key)
            return targetDate;

        return DateHelper.FindNearestDate(targetDate, comparisonSeries.AsOfDates);
    }
pagid
  • 13,559
  • 11
  • 78
  • 104
chuckd
  • 13,460
  • 29
  • 152
  • 331
  • Please be clear about what you want to do. I suppose you have a base class containing method ResolveDate (the second piece of code). Do you have a second class that derives from this base class and contains a method that overrides ResolveDate (first piece of code)? And then you want to test what exactly? – venerik Jun 05 '13 at 20:43
  • Hmm, if the first piece of code is your test, why don't you remove the call to the base method? Or replace it by something else. I don't see the point of using stub or shim. Off topic: the second piece contains implementation so the containing class is not abstract. – venerik Jun 05 '13 at 21:34
  • Hi.Sorry for th earlier responses. Yes I have a base class containing method ResolveDate (the second piece of code). Do you have a second class that derives from this base class and contains a method that overrides ResolveDate (first piece of code). I want to write a unit test for 'public override DateTime ResolveDate' and don't know if I should Stub or Shim out 'base.ResolveDate(comparisonSeries, targetDate);'? – chuckd Jun 06 '13 at 15:59

2 Answers2

2

To test a derived method in isolation from its base implementation, you need to shim it. Given the following system under test:

namespace ClassLibrary7
{
    public class Parent
    {
        public virtual string Method()
        {
            return "Parent";
        }
    }

    public class Child : Parent
    {
        public override string Method()
        {
            return base.Method() + "Child";
        } 
    }
}

You can write the following test for the Child.Method().

using ClassLibrary7;
using ClassLibrary7.Fakes;
using Microsoft.QualityTools.Testing.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Test
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            using (ShimsContext.Create())
            {
                var child = new Child();

                var shim = new ShimParent(child);
                shim.Method = () => "Detour";

                string result = child.Method();

                Assert.IsFalse(result.Contains("Parent"));
                Assert.IsTrue(result.Contains("Detour"));
                Assert.IsTrue(result.Contains("Child"));
            }
        }
    }
}

Note that the first two Asserts are included only to illustrate how the parent method is detoured. In a real test only asserts for the child method would be needed.

Oleg Sych
  • 6,548
  • 36
  • 34
-1

1) First add a reference to the actual dll you want to test example: ABC.Interfaces 2) Then expand your references and on the actual dll that should now be in your references right click and say "Add Fakes Assembly"

Visual studio will process the references and if it was successfull you should see a new reference called ABC.Interfaces.1.0.0.0.Fakes..

You will now be able to see the stub and shims been added to your methods.

Hope this helps!

gideonlouw
  • 417
  • 1
  • 5
  • 9