您好我正在尝试对包含实体集合的实体框架进行单元测试。如下定义:
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
是一个密封的类,因此无法扩展。