开发者

Rhino Mock OfType<>

开发者 https://www.devze.com 2023-01-22 12:08 出处:网络
Im trying to test this statement IStudentAssessmentUnitStatus res = student开发者_StackOverflow社区.UnitStatusList.OfType<IStudentAssessmentUnitStatus>().

Im trying to test this statement

IStudentAssessmentUnitStatus res = student开发者_StackOverflow社区.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


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消