本文介绍了将ChaiHttp与beforeEach或before方法一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个NodeJS Express应用程序,我想对它使用cookie进行单元测试.因此,我想使用一个beforeEach或before来创建Cookie.

I have an NodeJS Express application which I want to unit test which uses cookies. So I want to use a beforeEach or before to create the Cookie.

代码可以正常工作(但没有before方法):

Code which works without any problem (but without the before method):

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: '[email protected]', 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: '[email protected]', 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之前或之前没有每个支持?我在做什么错了?

It is completely ignoring my before or beforeEach (both methods don't work).Maybe chai-http does not have before or beforeEach support ?What am I doing wrong ?

重组后.

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: '[email protected]', 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();
                });
        });
    });
});

我仍然对诺言有误.

推荐答案

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

If this is usefull for someone, this is the final solution:


 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: '[email protected]', 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();
                });
        });
    });
});

这篇关于将ChaiHttp与beforeEach或before方法一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 13:04