-1

I have mock test created like this:

IGroup studentGrp = MockRepository.GenerateMock<IGroup>();
stubApplication.Stub(x => x.GetGroup("STUDENT"))
               .Return(studentGrp);
studentGrp.Stub(x => x.EntityCount)
          .Return(1);
stubApplication.Stub(x => x.GetGroup("STUDENT").GetEntity(0).GetField("role_numb"))
               .Return(genericFieldValue);

Code:

for(int i = 0; i < StudentApplication.GetGroup("STUDENT").EntityCount; i++)
{
    if (StudentApplication.GetGroup("STUDENT").GetEntity(i).GetField("role_num").GetInternalValue() == "Y")
    {
        //Do Something.. 
    }
}

But i am unable to run above code keep getting: NullReferenceException.

Old Fox
  • 8,629
  • 4
  • 34
  • 52
NoviceMe
  • 3,126
  • 11
  • 57
  • 117
  • 2
    might help debugging if you would use variables and separate statements instead of linking all those method calls, such as var entity = StudentApplication.GetGroup("STUDENT").GetEntity(i); var field = entity.GetField("role_num"); - you get the point – Alexandru Marculescu May 27 '16 at 06:46
  • 1
    @AlexandruMărculescu you offered the answer without know it... Rhinomocks doesn't support lambda aggregation.... I explained more in my answer – Old Fox May 28 '16 at 23:10

1 Answers1

2

RhinoMocks doesn't support lambda aggregation:

stubApplication.Stub(x => x.GetGroup("STUDENT").GetEntity(0).GetField("role_numb"))
           .Return(genericFieldValue);

The above snippet won't work in Rhinomocks(Moq support lambda aggregation).

You need to split the lambda:

stubApplication.Stub(x => x.GetGroup("STUDENT"))
           .Return(studentGrp);
studentGrp.Stub(x => x.GetEntity(0))
          .Return(fakeEntity);
fakeEntity.Stub(x => x.GetField("role_numb"))
          .Return(fakeField);
fakeField.Stub(x => x.GetInternalValue())
         .Return("Y");

BTW, just by reading the names of the objects and methods it seems that all instances are instances of PoCos, if so you don't need to fake them at all(use real instances...)

Old Fox
  • 8,629
  • 4
  • 34
  • 52