4

テストメソッドbase.ResolveDate()内に、基本クラスとそのパブリックおよび仮想からのメソッドがあります。このメソッドを自分のメソッドでスタブ/シムしたいのですが、スタブまたはシムしますか? スタブまたはシム、どうすればそれを行うことができますか? 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);
    }

スタブ/シムしたい基本クラスのメソッドは次のとおりです。

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

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

2 に答える 2

-1

1) まず、例をテストする実際の dll への参照を追加します: ABC.Interfaces 2) 次に、参照を展開し、参照にあるはずの実際の dll を右クリックして、「Add Fakes Assembly」と言います。

Visual Studio が参照を処理し、正常に処理された場合は、ABC.Interfaces.1.0.0.0.Fakes. という新しい参照が表示されます。

メソッドに追加されたスタブとシムを確認できます。

お役に立てれば!

于 2015-04-01T12:09:35.730 に答える