0

我试图对我的存储库进行单元测试。我需要用一些值来模拟 Where 子句。我试过以下

var parentClass = new ParentClass {id = 1};

var subClass = new SubClass
{
    id=2,
    ParentClassId = parentClass.id,
    ParentClass = parentClass
}

var subSubClass = new SubSubClass
{
    id =3,
    SubClassId = subClass.id,
    SubClass = subClass
}

var dbContext = MockRepository.GenerateMock<IDbContext>();
    var subClassContext = MockRepository.GenerateMock<IDbSet<SubClass>>();
var subSubClassContext = MockRepository.GenerateMock<IDbSet<SubSubClass>>();

subClassContext.Stub(x => x.GetEnumerator())
                .Return(null)
                .WhenCalled(c => c.ReturnValue = new List<SubClass> { subClass }.GetEnumerator());

subSubClassContext.Stub(x => x.GetEnumerator())
                .Return(null)
                .WhenCalled(c => c.ReturnValue = new List<SubSubClass> { subSubClass }.GetEnumerator());

我正在测试以下代码

var existingSubSubClasses = context.SubSubClass.Where(cba => cba.SubClass.ParentClassId == parentClassId).ToList();  

我收到 ArgumentNullReferenceException

我究竟做错了什么?

4

1 回答 1

-1

您还应该存根context.SubSubClass属性——它似乎null默认返回。

于 2013-09-20T12:21:40.900 回答