ViewResult returns no data
I am using the pattern from NerdDinner. I call Index() in my test Method开发者_如何学Go and I the ViewREsult I get back has no data. So the variable data ends up being null.
However, I know that there is data there. Because I can go to the watch window and expand the variable result and expand viewData->Model->ResultsView then I see the "expanding will result view will enumerate the IEnumerable" When I expand it, the data exists.
Any idea why the data comes back null unless I expand?
thanks Jas
[TestMethod]
public void Index__Should_Return_1_or_More_lessons()
{
var controller = new LessonController(new FakeLessonRepository());
var result = controller.Index() as ViewResult;
var data = result.ViewData.Model as IList<Lesson>;
Assert.IsTrue(data.Count > 0);
}
It's because of Lazy Loading in Linq or EF (depending on what you're using) The queries are only executed when needed. You can force it to be executed by calling a finalizer like ToList() or ToArray() or something similar.
精彩评论