Moq - 是否有可能从模拟中返回一个模拟?

时间:2017-10-26 00:47:42

标签: c# unit-testing moq

您好我正在尝试对包含实体集合的实体框架进行单元测试。如下定义:

public interface IRatesRepositoryEntities : IDisposable
{
    IObjectSet<StaticScenarioContainer> StaticScenarioContainers { get; }
}

public partial class StaticScenarioContainer : EntityObject
{
    public EntityCollection<StaticScenarioShockGroup> StaticScenarioShockGroups
}

我可以使用以下代码模拟StaticScenarioContainers

        var list = new[]
        {
            new StaticScenarioContainer
            {
                M_SCNLABEL = containerName
            }
        };
        var mockObjectSet = new FakeObjectSet<StaticScenarioContainer>(list);

        mockRatesRepositoryEntities.Setup(e => e.StaticScenarioContainers)
            .Returns(mockObjectSet);

FakeObjectSet实施IObjectSet并支持Linq次查询。

但是,当我尝试从下面的模拟StaticScenarioShockGroup代码中模拟StaticScenarioContainers失败时。请建议:

        var mockStaticScenarioShockGroups = new EntityCollection<StaticScenarioShockGroup>();
        var mockStaticScenarioContainer = new Mock<StaticScenarioContainer>();
        mockStaticScenarioContainer.Setup(e => e.M_SCNLABEL).Returns(containerName);
        mockStaticScenarioContainer.Setup(e => e.StaticScenarioShockGroups)
            .Returns(mockStaticScenarioShockGroups);

        var list = new[]
        {
            mockStaticScenarioContainer.Object
        };
        var mockObjectSet = new FakeObjectSet<StaticScenarioContainer>(list);

        mockRatesRepositoryEntities.Setup(e => e.StaticScenarioContainers)
            .Returns(mockObjectSet);

请注意EntityCollection是一个密封的类,因此无法扩展。

0 个答案:

没有答案