Includes feature in MicroLite 2.0 has made it easier to reduce your data access footprint by batching multiple database queries into a single call to load all the data you need in one go. This however has had a slight negative impact on the simplicity of testing code which uses MicroLite, this is because the include methods return interfaces rather than objects (in the way that
Now, since interfaces are being returned, it is still possible to test the methods by creating mocks of the interface which leads to code such as this:
var mockInclude = new Mock<IInclude<Customer>>(); mockInclude.Setup(i => i.Value).Returns(customer); mockSession .Setup(s => s.Include.Single<Customer>(customerId)) .Returns(mockInclude.Object);
While this works, we can do better (imagine if you were including 3 or 4 object in the method you are testing!).
Therefore, a new project has been released – MicroLite.Testing. The initial release has some classes to make testing includes easier.
If we install the MicroLite.Testing package and add a
using MicroLite.Testing; to our test class, we no longer have to mock the include interfaces and can do this instead:
mockSession .Setup(s => s.Include.Single<Customer>(customerId)) .Returns(Include.Single(customer));
Much better 🙂
There are 4 methods on the
Many<T>()– This method will return an Include many which contains no values to simulate no results for an include.
Many<T>(IList<T> values)– This method will return an Include many which contains the specified values to simulate results being returned.
Single<T>()– This method will return an Include with the default value of T (null for a reference type, 0 for an integer etc) to simulate no results for the include.
Single<T>(T value)– This method will return an Include with the specified value to simulate a single result being included.
This article was written for version 2.0.0 of the MicroLite framework and version 1.0.0 of the MicroLite.Testing package.