如何使用express,sequelize,mocha和supertests进行回滚unit testing
我试图做一个testing插入数据到数据库,testing一个请求与超级,然后回滚与假数据。 有人知道这种方式是正确的吗? 如果不是,请您回答并告诉我哪个方法正确? 提前致谢。
... var app = express(); var request = require('supertest'); var assert = require("assert"); var db = require('../../models'); var mysql = require('mysql'); describe('[Test controller - send_confirmation_email.js]', function () { describe('POST /crowdfunding/sendConfirmationEmail', function () { it('Second post test with data', function (done) { db.sequelize.transaction(function (t) { var cf = db.Crowdfunding.build({ money_raised: 80, project_id: 999, country: 'germany', type: 'SONG', state: 'PENDING' }); cf.save({ transaction: t }).success(function (cf) { request(app) .post('/crowdfunding/sendConfirmationEmail') .send({ 'id': cf.id, 'text': 'test text' }) // .expect(500) .end(function (err, res) { assert.equal('PENDING', cf.state); t.rollback(); done(); }); }).error(function () { }); }); }); }); });
这看起来像是你有正确的想法,但交易的控制stream程时机是不正确的。 这里有一些提示。
- 将事务设置移到带有
done
callback的before
处理程序,并且不要在cf.save().success(
callbackcf.save().success(
之前调用done
。这将确保事务在开始testing逻辑之前完成保存。 - 移动
t.rollback();
到after
处理程序或事务将回滚,然后发送您的testing请求。 - 不要忘记声明请求成功,没有错误和200级的状态码
- 在node.js中对unit testing中的依赖关系进行存根
- `lab`和`code`与`mocha`和`chai` nodejs库之间的比较
- 为所有testing用例(Mocha)添加一个默认的before()函数,
- testing在节点中使用mocha / supertestredirect的请求
- 我怎样才能让我的node.jsunit testing识别node_modules?
- 使用Mocha在NodeJS中进行OAuthstreamtesting
- 如何使用数据编写摩卡咖啡发布请求testing以testing响应是否匹配?
- 如何获得伊斯坦布尔与Sails.js 0.10.x?
- 在节点中模拟postgres调用