What is the correct way to unit test a method that returns a sequence of complex objects?
I’m implementing a branch finder. I have a repository that returns a Branch object which has various information such as
- Branch name
- Address
- Location
- Products stocked
I have a repository that I’ve faked to always return a known sequence of Branch objects and I need to test the logic in my BranchFinder service.
The Find method takes a location (lat/long) and a product specification in the form of a Flags enum .
public IEnumerable<Branch> Find(Location location, Products products);
Knowing the data that the search is going to operate on, I can safely specify a location and product specification and know what to expect, but how do I assert that the sequence is correct.
e.g.
var loc = new Location(53.79424, -1.546112);
var results = service.Find(loc, Products.Milk | Products.Bananas);
I know for this given query that a certain subset of data should be returned in a certain order, and also that the distance from the current location to the branch should increase as you iterate through the result set.
What is the correct/best way to test these rules and situations?
First thing I would do would be to make sure you access the Service via an interface
ILocatorService. Also make sure that any downstream dependency it has to get the Branch info also used via Interface (so that you can inject Mock later on in your test).Also, be clear that you are testing ILocatorService itself (to make sure that any change you made in the service doest break it).
For Any Dependencies that ILocatorService uses, you can use RhynoMocks to return set of results (it might be going to DB or some other vendor app to give us list of branches)
That way your inputs to ILocatorService stays the same and the only thing you are testing is if any logic that you have changed is breaking existign test/functionality.