One of the most important ways on how Chill helps with unit testing is by using an auto mocking container.
Compare the following code snippets:
There are a lot of things wrong with this example:
- The knowledge about which dependencies by your Subject are needed are duplicated among all your tests.
- In each test, you'll have to explicitly create mock objects. This clutters your test with code that does add any value.
- Multiple asserts per test make it more difficult to figure out what exactly goes wrong.
- No explicit structure to this test. What exactly is setup code.
- Even though most tests use a subject, a result and variables, the naming of these variables will be very different across different tests by different authors, making them more difficult to understand.
Compare this with a Chill example:
The automocking container sets up your Subject and automatically injects mock objects for any dependencies it needs. This is great if you have many tests against the same subject and you need to add a dependency. Your tests might fail, but that's exactly what you want. But they still compile and run!
There is no need to explicitly create mock objects anymore. The The<> method will create, register and return a mock object for you, ready for use.
If you want to explicitly register a value, you can use the SetThe<>().To() method to an object. There is also a shorthand for this: UseThe().
Note the use of the .With() extension method. This simple little extension method makes it easy to modify objects afther they have been built, in a very clean way.