我正处于测试 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
?