【问题标题】:Use ChaiHttp with beforeEach or before method将 ChaiHttp 与 beforeEach 或 before 方法一起使用
【发布时间】:2019-10-16 09:10:19
【问题描述】:

我有一个 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();
                });
        });
    });
});

我仍然收到有关承诺的错误。

【问题讨论】:

  • 为什么你的 beforeEach 在没有测试的描述中?看看这个链接。它显示了正确的测试结构mochajs.org/#hooks
  • 我重构了代码
  • 谢谢,在研究了您发送给我的异步数据库示例示例后,请参阅我的重组。
  • 您看到了什么具体错误?你能发布堆栈跟踪吗?
  • 已解决,问题是我要退回代理。因此,如果在代理被删除之前代码中的返回,那么它就像一个魅力。

标签: node.js typescript mocha.js chai


【解决方案1】:

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


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

【讨论】:

    猜你喜欢
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    相关资源
    最近更新 更多