我有一个NodeJS Express应用程序,我想对它使用cookie进行单元测试。因此,我想使用beforeEach或before来创建Cookie。
代码可以正常工作(但没有before方法):
import * as chai from 'chai';
import { expect } from 'chai'
import chaiHttp = require('chai-http');
import { app } from '../../server';
describe('Relaties', () => {
describe('Ophalen alle relaties met: GET /api/ehrm-klantnr/relatie', () => {
it('should get alle relaties', (done) => {
let agent = chai.request.agent(app)
agent
.put('/api/ehrm-klantnr/medewerker/login')
.send({ email: 'admin@sfgtest.com', wachtwoord: '<secret>' })
.then(function (res) {
expect(res).to.have.cookie('SESSIONID');
// The `agent` now has the sessionid cookie saved, and will send it
// back to the server in the next request:
return agent.get('/api/ehrm-klantnr/relatie')
.set('meta','1')
.then(function (res) {
expect(res).to.have.status(200);
expect(res.body.data[0].vestiging).to.equal('Slib Hoofdkantoor');
done();
});
});
});
});
});
这不运行是什么
import * as chai from 'chai';
import { expect } from 'chai'
import chaiHttp = require('chai-http');
import { app } from '../../server';
describe('Relaties', () => {
let agent = chai.request.agent(app);
describe('First this one', function () {
beforeEach(function () {
console.log('outer describe - beforeEach');
agent
.put('/api/ehrm-klantnr/medewerker/login')
.send({ email: 'admin@sfgtest.com', wachtwoord: '<secret>' })
.then(function (res) {
expect(res).to.have.cookie('SESSIONID');
});
});
});
describe('Ophalen alle relaties met: GET /api/ehrm-klantnr/relatie', () => {
it('should get alle relaties', (done) => {
return agent.get('/api/ehrm-klantnr/relatie')
.set('meta', '1')
.then(function (res) {
expect(res).to.have.status(200);
expect(res.body.data[0].vestiging).to.equal('Slib Hoofdkantoor');
done();
});
});
});
});
它完全忽略了我的before或beforeEach(两种方法都不起作用)。 也许chai-http之前或之前没有每个支持? 我在做什么错了?
重组后。
describe('Relaties', () => {
const agent = chai.request.agent(app);
beforeEach(function (done) {
console.log('outer describe - beforeEach');
agent
.put('/api/ehrm-klantnr/medewerker/login')
.send({ email: 'admin@sfgtest.com', wachtwoord: '<secret>' })
.then(function (res) {
expect(res).to.have.cookie('SESSIONID');
done();
});
});
describe('Ophalen alle relaties met: GET /api/ehrm-klantnr/relatie', () => {
it('should get alle relaties', (done) => {
return agent.get('/api/ehrm-klantnr/relatie')
.set('meta', '1')
.then(function (res) {
expect(res).to.have.status(200);
expect(res).to.be.an('object');
expect(res.body.data).to.be.an('array');
expect(res.body.data[0]).to.be.an('object');
expect(res.body.data[0].id).to.equal(1);
done();
});
});
});
});
我仍然对诺言有误。
答案 0 :(得分:0)
如果这对某人有用,这是最终解决方案:
describe('Relaties', () => {
const agent = chai.request.agent(app);
beforeEach(function (done) {
console.log('outer describe - beforeEach');
agent
.put('/api/ehrm-klantnr/medewerker/login')
.send({ email: 'admin@sfgtest.com', wachtwoord: '<secret>' })
.then(function (res) {
expect(res).to.have.cookie('SESSIONID');
done();
});
});
describe('Ophalen alle relaties met: GET /api/ehrm-klantnr/relatie', () => {
it('should get alle relaties', (done) => {
agent.get('/api/ehrm-klantnr/relatie')
.set('meta', '1')
.then(function (res) {
expect(res).to.have.status(200);
done();
});
});
});
});