gpt4 book ai didi

node.js - 将 ChaiHttp 与 beforeEach 或 before 方法一起使用

转载 作者:太空宇宙 更新时间:2023-11-04 01:30:05 24 4
gpt4 key购买 nike

我有一个 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 没有 before 或 beforeEach 支持?我做错了什么?

重组后。

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();
});
});
});
});

我仍然收到有关 promise 的错误。

最佳答案

如果这对某人有用,这是最终的解决方案:


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();
});
});
});
});

关于node.js - 将 ChaiHttp 与 beforeEach 或 before 方法一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56382865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com