我正处于测试Slack机器人手柄的红色阶段,因此我预计会出现错误。
我有以下代码:
generateHandler({ fetch }) {
return async function(bot, message) {
let status;
try {
let response = await fetch(endpoint, {
method: 'POST',
body: {build_parameters: { ALLOW_OVERWRITE: "true" }}
});
status = response.ok;
} catch (error) {
status = false;
}
return status;
}
}
我写了以下测试:
import test from 'ava';
import { expect } from 'chai';
const { generateHandler } = require('../lib/handlers/redeploy_master.js');
test('triggers a new build on circleci', async (t) => {
const fetch = async (endpoint, { method, body }) => {
const expectedBody = { build_parameters: { ALLOW_OVERWRITE: "true" } };
expect(method).to.be('POST');
expect(body).to.deep.equal(JSON.stringify(expectedBody));
return Promise.resolve({ok: true});
};
const handler = generateHandler({fetch});
const bot = {reply: () => {}};
return await handler(bot, undefined);
});
如果我用原生expect()
替换ava
断言:
t.true(endpoint.startsWith('http'));
t.is(method, 'POST');
t.deepEqual(body, JSON.stringify(expectedBody));
它抛出了我期待的错误:
10: t.is(method, 'POST');
11: t.deepEqual(body, JSON.stringify(expectedBody));
12: expect(method).to.be('POST');
Difference:
- {
- build_parameters: Object { … },
- }
+ '{"build_parameters":{"ALLOW_OVERWRITE":"true"}}'
我收到以下错误:
Test finished without running any assertions
使用assert
提供相同的行为
假设chai
条款中的try…catch
断言被捕获,而来自ava
的断言不是吗?