Tag: tdd
-
Mocking FunctionContext GetLogger using Durable Functions Isolated Worker
Although you can inject your ILogger<T> into the constructor of your function, sometimes you may want to use the FunctionContext from your trigger method. Mocking this for testing can be a bit tricky. Consider the following function: We want to write tests for our class. For this to be testable, we’ll need to somehow mock…
-
Better assertions when verifying Moq’d methods have been called
Moq, according to its GitHub page, is “The most popular and friendly mocking framework for .NET”. Mocking frameworks allow you to create a ‘fake’ version of a dependency (typically an interface, or abstract class). With this fake, you can then instruct the methods return values, or invoke behaviour. Alternatively you can assert the methods were called,…
-
Asserting multiple properties of argument with FakeItEasy
More often than not, I want to assert that my dependency has been called with a parameter that matches more than one condition. Let’s say we have a method that calls ICustomerService.Add method. We want to assert the Add method was called with a Customer that matches a few conditions. The way we’d typically achieve…
-
Stubbing middleware when testing an Express with Supertest
When testing a simple express app / api I like to use supertest. This can feel like integration testing, and to an extent it is. But we can stub things out with sinon, and tidy up our tests. Let’s look at an example. Suppose we have a simple API with the following router: var express…
-
.net project unit test naming
Just wanted to do a quick post on unit test method naming, in particular with .net projects; Most test suites I come across are title cased: public void LikeThis() This annoys me. I find long method names hard to read, and easy to miss detail Example: public void DoesNotReImplementAdditionalInterfaceAlreadyImplementedByInterceptedClass() is not a readable as: public…
-
How to assert.throws on an async function with a callback
Recently, I was writing a module that took a callback, and needed to write a test, asserting on an exception. Here’s how to do it: it(‘throws error if myParam is less than 10’, function(done) { var fn = function(){ myModule.doSomething(8, function(err) { if (err) throw err }); } assert.throws( function() { fn() }, /Value is…
-
Unit tests failing when run all together with FakeItEasy
While running my tests, I noticed that if I ran my test suite all together, some would fail, giving the exception: API restriction: The assembly has already loaded from a different location This issue was happening both in ReSharper test runner, and using the NUnit test runner Debugging, the exception was being thrown on my…