How to Test That MVC DocumentProvider Has Returned Items

Eric Rovtar asked on March 8, 2016 23:39

Hi!

When building an MVC site, what is the best method to test that an item has been returned by a DocumentProvider?

Is the best method to use try/catch like I have below:

        try
        {
            //Get Kentico Object
            //Kentico Object with Retrieve CCB data and store in the CcbEventData property
            var ev = (from e in CcbEventProvider.GetCcbEvents().Published()
                      where e.EventName == eventName
                      select e).First();

            return View(ev);
        }
        catch (InvalidOperationException ex)
        {
            throw new HttpException(404, "Event Not Found");
        }

I just want to be sure I'm handling this the best way.

I could always call Count() first, but then I'm technically making two queries, which seems to add unnecessary overhead.

Thanks!

-Eric

Recent Answers


Dennis Hulsmans answered on March 9, 2016 13:18 (last edited on March 9, 2016 13:19)

Hi,

can't you use .Any() ? This is a LINQ function and it doesn't iterate the list like the .Count() does. So it's performance is way better then .Count().

1 votesVote for this answer Mark as a Correct answer

Eric Rovtar answered on March 9, 2016 15:40

Thanks, Dennis.

Wouldn't that still make two database calls, though? That's still what I'm trying to avoid.

0 votesVote for this answer Mark as a Correct answer

Dennis Hulsmans answered on March 11, 2016 10:23 (last edited on March 11, 2016 10:23)

Hi Eric,

When you have your list, the .Any() function does an in memory check in the list (the list is in memory) so there is no extra database call

1 votesVote for this answer Mark as a Correct answer

Eric Rovtar answered on March 12, 2016 05:50

Thanks, Dennis. I didn't know that.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.