I needed to figure out a way to unit test some code that makes use of Lazy to instanciate its dependencies. I think Lazy sucks but it has its place and I’ve seen first hand massive performance gains in monolithic applications when implemented correctly.
Constructor Injection
To unit test a method that uses a property that is instantiated with Lazy in C#, you can use the following steps:
- Create a mock of the
Lazy
property. Moq is my go to although the recent SponsorLink drama may change that. - Set the mock to return a specific value. This will allow you to test the behavior of the method under different conditions.
- Call the method under test.
- Verify that the method behaves as expected. You can do this by asserting the values of any output parameters or the state of any external objects.
Example
1 | using Moq; |
In this example, I mocked the FooService
property. This allows us to control the behavior of the DoWork()
method without having to create a real instance of the FooService
class.
Here I used the Verify()
method to assert that the FooServiceMethod()
method on the FooService
mock was called. This means that the DoWork()
method under test behaved as expected.
Property Injection
If the constructor is not used to instantiate the Lazy property, you can still unit test the method that uses it by following these steps:
- Create a new instance of the class that contains the Lazy property.
- Set the Lazy property to a mock of the
FooService
class. - Call the method under test.
- Verify that the method behaves as expected.
Example:
1 | using Moq; |
In this example, I created a new instance of the SweetClass
class and setting the FooServiceQuery
property to a mock of the FooService
class. This allows control over the behavior of the DoWork()
method without having to create a real instance of the FooService
class.
Then Verify()
is used again the same as the constructor injection example.
Sweetbix.