views:

53

answers:

1

Hi Guys,

I have the following two methods on an interface for my service layer:

ICollection<T> FindAll<T>(Expression<Func<T, bool>> predicate) where T : Post;
ICollection<T> FindAll<T,TKey>(Expression<Func<T, bool>> predicate, OrderingOptions<T,TKey> orderingOptions) where T : Post;

They are extremely similar.

The first one (basically) does this:

return repository
   .Find()
   .OfType<T>()
   .Where(predicate)
   .Take(maxRows)
   .ToList();

The second one (basically) does this:

return repository
   .Find()
   .OfType<T>()
   .Where(predicate)
   .WithOrdering(orderingOptions)
   .Take(maxRows)
   .ToList();

WithOrdering is a pipe extension method i created:

public static IOrderedQueryable<T> WithOrdering<T,TKey>(this IQueryable<T> source, OrderingOptions<T,TKey> postOrderingOptions)
        {
            return postOrderingOptions.SortAscending
                       ? source.OrderBy(postOrderingOptions.OrderingKey)
                       : source.OrderByDescending(postOrderingOptions.OrderingKey);

        }

OrderingOptions<T,TKey> is a generic class i implemented to provide fluent ordering.

So, that being said - i have numerous unit tests for the first method (ensure collection is returned, ensure null is returned for an empty collection, ensure filtered correctly, ensure max rows is returned, etc).

But my question is - how do i test the second method? Do i need two? Given that the only thing that is different is that im using the .OrderBy (or .OrderByDescending) methods.

If im testing that, am i simply testing the LINQ framework?

If not, how can i even test this?

Couple of (possibly important) points:

  1. This are methods on a service layer, which retrieve IQueryable's on a repository, that is implemented with Entity Framework 4.0
  2. For my unit-tests, the service layer is injected (via StructureMap) a mock repository, but i also toggle this to the real repository manually when i want to do integration testing

Guidance/best practices would be highly appreciated.

Thanks!

+2  A: 

You need to unit-test the second method just to make sure it works. If you refactor something that changes how it works and fix the first one, how will you know if you forgot to fix the second one? If you refactor WithOrdering, how will you know that things still work? I would have a unit test to test ascending and one to test descending.

Gabe
Agreed. But `how` can i unit test that? Will i have to (for example) grab say 3 items that i know the date for (using a method to get a single item), then grab 3 items ordered by date (using the second method), and compare the values manually? (ie ordered list items[0].date == firstItem.date?)
RPM1984
Gabe
Yep, that's what i thought. Okay, cheers. +1, but i'll leave the question open for a bit (just so i can see other's thoughts). Thanks.
RPM1984