views:

27

answers:

1

Hi!

I am using the pattern from NerdDinner. I call Index() in my test Method 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);
    }
A: 

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.

BuildStarted
hmm not sure how to get to the ToArray or ToList methods on ViewResult... Any ideas?
Jas
`var data = ((IList<Lesson>)result.ViewData.Model).ToList();` should probably work just fine
BuildStarted
Thanks! var data = ((EnumerableQuery<Lesson>)result.ViewData.Model).ToList(); did the trick.
Jas
Glad you got it. :)
BuildStarted