Skip to content

Testing a Cake Addin

Sponsor: Using RabbitMQ or Azure Service Bus in your .NET systems? Well, you could just use their SDKs and roll your own serialization, routing, outbox, retries, and telemetry. I mean, seriously, how hard could it be?

Learn more about Software Architecture & Design.
Join thousands of developers getting weekly updates to increase your understanding of software architecture and design concepts.


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.

ICakeContext

Since 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 ICakeContext you use within your extension method.  In my example, the only thing I was using was the Log property. For reference, here was my extension method:

FakeCakeContext

I’m going to create a FakeCakeContext for my tests that will implement ICakeContext.  All the properties I need to implement I will be using FakeItEasy to create fakes.

Test

Now 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.Write was actually called on the FakeCakeContext.

FakeItEasy

I’ve been using FakeItEasy a lot more lately instead of Moq and have really been enjoying it.  If you are using a mocking or faking library, which do you prefer?  Let me know in the comments or on Twitter.