RhinoMock Stub Linq用于DbContext的Where子句

时间:2013-09-20 03:35:06

标签: c# linq entity-framework rhino-mocks stubbing

我试图对我的存储库进行单元测试。我需要用一些值来模拟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

我做错了什么?

1 个答案:

答案 0 :(得分:-1)

您还应该存根context.SubSubClass属性 - 默认情况下它似乎返回null