从具有存根的接口设置属性

时间:2013-05-31 21:19:05

标签: c# unit-testing microsoft-fakes

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

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

1 个答案:

答案 0 :(得分: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);
}