views:

29

answers:

1

Hi,

Im trying to test this statement

IStudentAssessmentUnitStatus res = student.UnitStatusList.OfType<IStudentAssessmentUnitStatus>().
                                                                                FirstOrDefault(s => s.ID == unit.ID);

Inside the list there could be multiple types hence the OfType. However when testing it says "Object reference not set to an instance"

     var _mockStudentFormUnit = _mockery.DynamicMock<IStudentAssessmentUnitStatus>();
     var _mockStudentAssessmentUnit = _mockery.DynamicMock<IStudentFormUnitStatus>();

     var studentunitList = new List<IStudentUnitStatus>() { _mockStudentFormUnit, _mockStudentAssessmentUnit };

     var mockEnum2 = _mockery.DynamicMock<IEnumerable<IStudentUnitStatus>>();

     Expect.Call(_mockStudent2.UnitStatusList).Return(mockEnum2).Repeat.Any();
     Expect.Call(mockEnum2.GetEnumerator()).Return(null).WhenCalled(s => s.ReturnValue = studentunitList.GetEnumerator()).Repeat.Any();

Can any Rhino experts see what I have done wrong . The above works fine for enumerations and OfType technically should just do a foreach and perform an "is" operation

Thanks

A: 

Try replacing the last line with:

Expect.Call(mockEnum2.GetEnumerator()).Do(new Func<IEnumerator<IStudentUnitStatus>>(s => studentunitList.GetEnumerator())).Repeat.Any();

(You may have to change IEnumerator<IStudentUnitStatus> to IEnumerator to get this to work.)

Pieter