0

I was trying to unit test my repository. I need to mock the Where Clause with some values. I have tried the following

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

I am testing the following code

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

I am getting ArgumentNullReferenceException

What am I doing wrong?

Saanch
  • 1,814
  • 1
  • 24
  • 38
  • The code in your two code fragments don't directly relate to each other. What is `context` in the second fragment? It is `dbContext` from above, or something different? – Chris Mantle Sep 20 '13 at 16:20

1 Answers1

-1

You should also stub context.SubSubClass property -- seems like it returns null by default.

the_joric
  • 11,986
  • 6
  • 36
  • 57