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:
Guidance/best practices would be highly appreciated.
Thanks!
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.