【问题标题】:Do I Stub or Shim a method inside my test method?我是否在我的测试方法中存根或填充一个方法?
【发布时间】:2013-06-05 19:00:03
【问题描述】:

我的测试方法中有一个方法base.ResolveDate(),它来自基类及其公共和虚拟。我想用我自己的方法来存根/填充这个方法,那么我应该存根还是填充? Stub 或 Shim,我该怎么做呢?根据我对 MS Fakes 的经验,它似乎是一个存根,因为 存根只能影响可覆盖的方法。 - ALM 2012

这里是测试方法:

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);
    }

这是我想要 Stub/Shim 的基类中的方法吗?

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

        return DateHelper.FindNearestDate(targetDate, comparisonSeries.AsOfDates);
    }

【问题讨论】:

  • 请清楚你想做什么。我想你有一个包含方法 ResolveDate 的基类(第二段代码)。您是否有派生自此基类并包含覆盖 ResolveDate(第一段代码)的方法的第二个类?然后你想测试到底是什么?
  • 嗯,如果第一段代码是你的测试,为什么不去掉对基方法的调用呢?或者用别的东西代替。我看不出使用存根或垫片的意义。题外话:第二部分包含实现,所以包含的类不是抽象的。
  • 嗨。很抱歉之前的回复。是的,我有一个包含方法 ResolveDate(第二段代码)的基类。您是否有从该基类派生的第二个类,并包含一个覆盖 ResolveDate(第一段代码)的方法。我想为 'public override DateTime ResolveDate' 编写一个单元测试,但不知道是否应该 Stub 或 Shim out 'base.ResolveDate(comparisonSeries, targetDate);'?

标签: c# unit-testing microsoft-fakes stubs


【解决方案1】:

要独立于其基本实现来测试派生方法,您需要对其进行填充。给定以下被测系统:

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

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

您可以为 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"));
            }
        }
    }
}

请注意,包含前两个 Assert 只是为了说明如何绕过父方法。在实际测试中,只需要子方法的断言。

【讨论】:

    【解决方案2】:

    1) 首先添加对您要测试的实际 dll 的引用示例:ABC.Interfaces 2)然后展开您的引用并在现在应该在您的引用中的实际 dll 上右键单击并说“添加假货程序集”

    Visual Studio 将处理引用,如果成功,您应该会看到一个名为 ABC.Interfaces.1.0.0.0.Fakes..的新引用。

    您现在可以看到存根和垫片已添加到您的方法中。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 2019-07-29
      相关资源
      最近更新 更多