2010-10-26 119 views
1

我试着来测试这一说法犀牛模拟OfType <>

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

在列表内可以有多种类型因此OfType。但是在测试时它说:“对象未设置为实例”

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

任何犀牛专家可以看到我做了什么错。以上为枚举和OfType工作正常,在技术上应该只是做一个foreach并执行“是”操作

感谢

+0

student.UnitStatusList的类型是什么? ,你能告诉我们吗? – TalentTuner 2010-10-26 03:43:53

+0

您要测试的行包含两个调用:调用扩展方法“OfType”,然后调用扩展方法“FirstOrDefault”。这两者都是.NET Framework调用,因此您只是测试.NET Framework(可能不是您想要测试的内容)。目前还不清楚“学生”是什么,但是如果UnitStatusList是一个虚拟财产,那么您应该没有什么问题可以用Rhino Mocks来解决这个问题。 – PatrickSteele 2010-10-26 20:05:57

回答

1

尝试用替换最后一行:

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

(您可能需要改变IEnumerator<IStudentUnitStatus>IEnumerator以使其起作用。)