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並執行「是」操作
感謝
student.UnitStatusList的類型是什麼? ,你能告訴我們嗎? – TalentTuner 2010-10-26 03:43:53
您要測試的行包含兩個調用:調用擴展方法「OfType」,然後調用擴展方法「FirstOrDefault」。這兩者都是.NET Framework調用,因此您只是測試.NET Framework(可能不是您想要測試的內容)。目前還不清楚「學生」是什麼,但是如果UnitStatusList是一個虛擬財產,那麼您應該沒有什麼問題可以用Rhino Mocks來解決這個問題。 – PatrickSteele 2010-10-26 20:05:57