In my last post I created a simple Cake Addin that was for replacing appSettings in a app/web.config. As promised, one of the other aspects of a creating a cake adding will be writing tests. So let’s cover how you can get start testing a Cake addin.
Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.
ICakeContextSince we are creating an extension method on the
ICakeContext, we need an implementation we can use to test. One solution to this is to create a fake. Ultimately what you need to do is implement the aspects of the
ICakeContextyou use within your extension method. In my example, the only thing I was using was the
Logproperty. For reference, here was my extension method:
FakeCakeContextI’m going to create a
FakeCakeContextfor my tests that will implement
ICakeContext. All the properties I need to implement I will be using FakeItEasy to create fakes.
TestNow that we have our
FakeCakeContext, we can write a test that calls our extension method. In our test, I’m using FakeItEasy to validate that the
Log.Writewas actually called on the
One thought to “Testing a Cake Addin”
Testing for the log message is not really the important thing here, but I get the idea 😉 Besides I wouldn’t vote for mocking the ICakeContext. Those kinds of tests depend very much on assumptions about the contract, which can be wrong / change over time. I think that integration-testing this should be preferred.