Обновление : я пробовал supertest вместо chai-Http, и у него та же проблема.Поэтому я думаю, что проблема от superagent, потому что оба зависят от этого. Проблема решена с помощью JSON.stringify
chai.request(server)
.post('/auth/signup')
.send(JSON.stringify(requestBody))
.end(function (err, res) {
if (err) done(err);
expect(res.body).to.has.property('status');
expect(res.body.status).to.equal('error');
done();
});
Я работаю над веб-API с помощью sailsjs.Я начинаю писать тест для API, используя mocha, chai и chai-Http.Когда я отправляю данные через API, мокко не прекращается.но когда я ничего не отправляю, mocha завершается.
Я искал эту проблему, но не нашел ни одной проблемы или проблемы, подобной этой.
Мои версии среды и инструментов:
OS: (GNU/Linux) fedora 30
node: 10.16.1
npm: 6.11.3
sailsjs: 1.1.0
sails-hook-sequelize: 1.2.0
sequelize: 4.38.0
mocha: 6.2.0
jest: 24.9.0
chai: 4.2.0
chai-http: 4.3.0
Вот код
// set environement to test, to use test configuration
process.env.NODE_ENV = 'test'
const sails = require('sails');
const chai = require('chai');
const expect = chai.expect;
const chaiHttp = require('chai-http');
chai.use(chaiHttp);
let server;
// Before running any tests...
before(function(done) {
const wait = 10000;
console.log(`wait ${wait / 1000}sec. until sails complete loads...`);
// Increase the Mocha timeout so that Sails has enough time to lift, even if you have a bunch of assets.
this.timeout(wait);
sails.lift({
// Your Sails app's configuration files will be loaded automatically,
// but you can also specify any other special overrides here for testing purposes.
// For example, we might want to skip the Grunt hook,
// and disable all logs except errors and warnings:
// hooks: { grunt: false },
hooks: {
"blueprints": false,
"orm": false,
"pubsub": false,
"grunt" : false
},
log: { level: 'warn' },
}, function(err) {
if (err) return done(err);
server = sails.hooks.http.app;
// here you can load fixtures, etc.
// (for example, you might want to create some records in the database)
return done();
});
});
after(async function() {
return sails.lower();
});
describe('#Auth Controller - Register', () => {
it('signup with lessest data', function() {
const requestBody = {
name: 'Zaid-2',
phone: '0020110123456700',
password: '123456',
category: 'customer',
};
return chai.request(server)
.post('/auth/signup')
.send(requestBody)
.then(function(res) {
expect(res.body).to.has.property('status');
expect(res.body.status).to.equal('success');
});
// check that user created successfully in the database
// models are in sails.models
});
});
, когда я удаляю .send
или использую его с пустыми данными, такими как .send()
, mocha завершается.
it('signup with lessest data', function() {
const requestBody = {
name: 'Zaid-2',
phone: '0020110123456700',
password: '123456',
category: 'customer',
};
return chai.request(server)
.post('/auth/signup')
.send() // send no data
.then(function(res) {
expect(res.body).to.has.property('status');
expect(res.body.status).to.equal('success');
});
});