0

我正在创建一个存根接口并将其传递给我的测试方法,如下所示。我正在对传递给测试方法的接口(StubISeries)进行存根。如果comparisonSeries.Key只是接口中的获取,我可以在存根中设置值而不向接口添加设置属性吗?

var series = new SomeNameSpace.Fakes.StubISeries();
series.Key = something to set the key here?
DateTime date = formReport.ResolveDate(series, DateTime.Now);

public override DateTime ResolveDate(ISeries comparisonSeries, DateTime targetDate)
{
    switch (comparisonSeries.Key)
    {
        case SeriesKey.SomeKey1:
        case SeriesKey.SomeKey2:
        case SeriesKey.SomeKey3:
        case SeriesKey.SomeKey4:
        case SeriesKey.SomeKey5:
            return DateHelper.PreviousOrCurrentQuarterEnd(targetDate);
        default:
            break;
    }

    return base.ResolveDate(comparisonSeries, targetDate);
}
4

1 回答 1

0

给定ISeries接口和SeriesKey枚举的以下定义

public enum SeriesKey
{
    SomeKey1,
    SomeKey2,
    SomeKey3
}

public interface ISeries
{
    SeriesKey Key { get; }
}

您可以使用 Fakes 生成的 StubISeries 编写以下测试。

[TestMethod]
public void MyTestMethod()
{
    var stub = new StubISeries();
    stub.KeyGet = () => SeriesKey.SomeKey1;
    Assert.AreEqual(SeriesKey.SomeKey1, ((ISeries)stub).Key);
}
于 2013-06-17T15:36:10.817 回答