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

标签 javascript node.js unit-testing tap

有人可以提供有关如何使用 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/

相关文章:

javascript - 使用 JavaScript 在 Internet Explorer 中插入外部 CSS

node.js - 如何更新 rethinkDB 中的嵌套对象

c# - 如何对工厂方法进行单元测试?

javascript - 在 deviceReady sencha touch cordova 应用程序中加载 app.js

javascript - 如何在服务器端渲染期间检查窗口/ block 宽度?

node.js - 在 nodejs 上循环 redis 返回的 promise

javascript - mongoosejs 如何对不同的查询进行排序?

ios - 将单元测试目标添加到现有 Xcode 项目,无法在 bundle 中找到资源

javascript - 如何监视函数中调用的属性?

javascript - 如何连接从api接收到的字符串