Building Enterprise JavaScript Applications
上QQ阅读APP看书,第一时间看更新

Asserting the correct response payload content

Now, on to our last test. We need our error object payload to contain a message property that reads "Payload should not be empty". So first, let's implement our test:

Then('contains a message property which says "Payload should not be empty"', function () {
if (payload.message !== 'Payload should not be empty') {
throw new Error();
}
});

Next, run the tests again and they should fail. Then, to make it pass, we need to pass a different object into the res.end method. Your if block should now look like this:

if (req.method === 'POST' && req.url === '/users') {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
message: 'Payload should not be empty',
}));
return;
}

Now, when we run our E2E tests again, they all pass:

$ yarn run test:e2e
......

1 scenario (1 passed)
6 steps (6 passed)