Tagtdd

Better assertions when verifying Moq’d methods have been called

B
Man wearing fake glasses and moustache

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, with a certain set...

Asserting multiple properties of argument with FakeItEasy

A

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 this is by doing something like this: C# var fake = A...

Stubbing middleware when testing an Express with Supertest

S

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: app.js JavaScript var express = require('express'), app = express(); var router = express.Router(); app.use('/'...

.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() 1 public void LikeThis() This annoys me. I find long method names hard to read, and easy to miss detail Example: public void DoesNotReImplementAdditionalInterfaceAlreadyImplementedByInterceptedClass() 1 public void...

How to assert.throws on an async function with a callback

H

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: JavaScript 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 less than 10/ ) done(); }); 12345678910 it('throws error if...