gpt4 book ai didi

javascript - 如何在node-tap中使用beforeEach?

转载 作者:搜寻专家 更新时间:2023-10-31 23:52:35 25 4
gpt4 key购买 nike

有人可以提供有关如何使用 beforeEach 的示例吗? http://www.node-tap.org/api/理想情况下,promise 版本的示例,但回调版本的示例也很好。

这是我创建的测试,效果很好:

'use strict';

const t = require('tap');
const tp = require('tapromise');
const app = require('../../../server/server');
const Team = app.models.Team;

t.test('crupdate', t => {
t = tp(t);

const existingId = '123';
const existingData = {externalId: existingId, botId: 'b123'};
const existingTeam = Team.create(existingData);

return existingTeam.then(() => {
stubCreate();

const newId = 'not 123'
const newData = {externalId: newId, whatever: 'value'};
const newResult = Team.crupdate({externalId: newId}, newData);

const existingResult = Team.crupdate({externalId: existingId}, existingData);

return Promise.all([
t.equal(newResult, newData, 'Creates new Team when the external ID is different'),
t.match(existingResult, existingTeam, 'Finds existing Team when the external ID exists')
]);
});
})
.then(() => {
process.exit();
})
.catch(t.threw);


function stubCreate() {
Team.create = data => Promise.resolve(data);
}

在我做任何事情之前,我想保留 existingTeam。保存后,我想 stub Team.create。这两件事之后,我想开始实际测试。我认为如果不使用 Promise.all 或者可能复制测试代码,我可以使用 beforeEach 会更干净。

我如何将其转换为使用 beforeEach?或者它的用法示例是什么?

最佳答案

简单,回调函数返回promise

const t = require('tap');
const tp = require('tapromise');
const app = require('../../../server/server');
const Team = app.models.Team;

const existingId = '123';
const existingData = {
externalId: existingId,
botId: 'b123'
};

t.beforeEach(() => {
return Team.create(existingData).then(() => stubCreate());
});

t.test('crupdate', t => {
t = tp(t);

const newId = 'not 123'
const newData = {
externalId: newId,
whatever: 'value'
};
const newResult = Team.crupdate({
externalId: newId
}, newData);

const existingResult = Team.crupdate({
externalId: existingId
}, existingData);

return Promise.all([
t.equal(newResult, newData, 'Creates new Team when the external ID is different'),
t.match(existingResult, existingTeam, 'Finds existing Team when the external ID exists')
]);
}).then(() => {
process.exit();
}).catch(t.threw);


function stubCreate() {
Team.create = data => Promise.resolve(data);
}

关于javascript - 如何在node-tap中使用beforeEach?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38298505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com