我正在尝试将数据插入数据库进行测试,使用 supertest 测试请求,然后使用假数据进行回滚。有人知道这种方式是否正确吗?如果不是,请您回答并告诉我哪种方法是正确的?提前致谢。
...
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 () { });
});
});
});
});