Reputation: 6638
Assume I have the following class:
public class Person
{
public string FirstName{get;set;}
public string LastName{get;set;}
public int Age{get;set;}
}
Assume I have this query:
var persons = someListOfPersons.Where(r => r.Age > 18)
.Select(m => new Person(){LastName = m.LastName});
As we can see above, I am selecting new Person
objects with only the LastName
property actually selected. Note that at this point this is still just an IQueryable
.
How can I unit test this, to make sure that the LastName
property has been included in the select statement?
Upvotes: 0
Views: 545
Reputation: 750
I would focus on testing the actual behavior of the piece of code you are trying to test through its public contract. Testing implementation details like the type of returned objects, whether a getter was called, etc. ties your tests very close to the implementation, which means that any change in the code will require a change in the tests. This is something you want to minimize, or the cost of maintaining your unit tests will rise. You can find a nice article about testing behavior instead of implementation details here.
Just as Leslie Davies mentions, I would also provide a dummy list of persons in my test and check whether the piece of code correctly returns only the last names of persons that are over 18.
Sidenote: If it's important that you only expose the last names through this piece of code, why not redesign the code so that it only returns names instead of incomplete Person objects? Returning Person objects that are only partly initialized may lead to confusion further down the road.
Upvotes: 1