来自之前的代码在'itpec之后运行'

时间:2016-06-20 17:36:42

标签: mocha bdd supertest

我正在使用mocha和supertest来测试REST服务。在这种情况下,我需要测试GET请求后的后续登录是否从该GET请求返回查询。

  1. 登录用户
  2. 使用params
  3. 在端点上执行GET请求
  4. 登录用户并期望来自GET请求的params作为回复。
  5. 由于上述测试将针对各种端点进行多次,因此我提出了这个问题:

    /*
     * getRequest.js
     */
    'use strict';
    
    var request = require('supertest')
        , verifyUrl = require('./verifyUrl') // verifies basic url formatting
        ;
    
    module.exports = getRequest;
    
    /**
     * Perform GET request using given parameters and call next when finished.
     * @param getEnvironment {json}
     * @param getEnvironment.url {string}
     * @param getEnvironment.endPoint {string}
     * @param getEnvironment.authorization {string}
     * @param next {function} The callback
     */
    function(getEnvironment, getParams, next) {
        var url = verifyUrl(getEnvironment.url);
        request(url)
            .get(getEnvironment.endPoint)
            .set("authorization", getEnvironment.authorization)
            .set('Accept', 'application/json')
            .query(getParams)
            .end(next)
    }
    
    /*
     * loginWithCallback.js
     * 
     * Performs a basic login but depends on the callback to test the results of the login.
     */
    'use strict';
    
    var request = require('supertest')
        , VerifyUrl = require('./VerifyUrl')
        ;
    
    module.exports = LoginWithCallback;
    
    /**
     * Perform basic login, then call next passing in err and res for testing.
     *
     * @param setEnvironment {json}  Expects url and authorization. Any other parameters will be ignored.
     * @param setEnvironment.url {string} The url being tested.
     * @param setEnvironment.authorization {string} 
     * @param next {function} callback function that will perform the actual test.
     */
    function LoginWithCallback(setEnvironment, next) {
    
        var url = VerifyUrl(setEnvironment.url);
    
        request(url)
            .post('api/users/login')
            .set('authorization', setEnvironment.authorization)
            .set('Accept', 'application/json')
            .end(next);
    }
    
    /*
     * e2eTest.js
     */
    'use strict';
    
    var base64_encode = require('Base64').btoa
        , should = require('should')
        , jsonValidator = require('is-my-json-valid')
        , mergeJSON = require('lodash/merge')
        , lodashClone = require('lodash/clone')
        , responseSchema = require('./responseSchemas/200.json')
        , login = require('./loginWithCallback')
        , getRequest = require('./getRequest')
        ;
    
    var username = "newUser" + Date.now()
        , password = "passW0rd"
        , testEnvironment = {
            "url": "http://localhost:9000/",
            "endPoint": "api/users/login",
            "authorization": "Basic " + base64_encode(username + ":" + password)
        }
        ;
    
    var expectedResult = {};
    
    describe('End to End testing on' + JSON.stringify(testEnvironment), function () {
        describe('new user, ' + username + ', login', function () {
            it('should return 200 and an empty body message.', function (done) {
                login(testEnvironment, function (err, res) {
                    if (err) {
                        done(err);
                    }
    
                    res.statusCode.should.deepEqual(200);
    
                    var jsonValidate = jsonValidator(responseSchema, {verbose: true});
                    jsonValidate(res.body).should.be.true("Response body failed schema check: \n" +
                        JSON.stringify(jsonValidate.errors, null, 4));
    
                    res.body.should.deepEqual(expectedResult);
    
                    done();
                });
            });
        });
    
    
        describe('user, ' + username + ', logs in after performing get request', function () {
            var getRequestParams = {"firstName":"john", "lastName":"doe"};
    
            beforeEach(function() {
                var getEnviron = lodashClone(testEnvironment);
                getEnviron.endPoint = 'api/persons/findName';
                mergeJSON(expectedResult, {"persons": {"findName": getRequestParams}});
                getRequest(getEnviron, getReqestParams, function(err, res) {
                    if (err) {
                        done(err);
                    }
                    console.log("GET request: " JSON.stringify(res, null, 2));
                    done();
                });
            });
    
            it('should return 200 and query values', function(done) {
                 login(testEnvironment, function (err, res) {
                 if (err) {
                     done(err)
                 }
    
                 console.log("it test: " + JSON.stringify(res, null, 2));
    
                 res.statusCode.should.deepEqual(200);
    
                 var jsonValidate = jsonValidator(responseSchema, {verbose: true});
                 jsonValidate(res.body).should.be.true("Response body failed schema check: \n" + JSON.stringify(jsonValidate.errors, null, 4));
    
                 res.body.should.deepEqual(expectedResult);
    
                 done();
    
            });
        });
    });
    

    以上三个文件应该做的是以下内容: 1.创建一个新用户 2.登录新用户并测试其没有先前查询参数的响应。 (通过) 3.执行GET请求,然后在之前的块中打印结果 4.执行用户打印和测试结果的登录。

    但我得到的是:

    End to End testing on{"url":"http://localhost:9000/","endPoint":"api/users/login","authorization":"Basic bmV3VXNlcjE0NjY0NDI0OTEzNDc6cGFzc1cwcmQ="}
      new user, newUser1466442491347, login
        ✓ should return 200 and an empty body message. (54ms)
    
      user, newUser1466442491347, logs in after performing persons/findByName request
    it test: [res text]
        1) should return and query values
    GET request: [res text]
    
    1 passing
    1 failing
    
         Uncaught AssertionError: expected Object {} to equal Object {
      persons: Object { findByName: Object { firstName: 'joe', lastName: 'jones' } }
     }
    

    可以看出,'it'块似乎在before块完成之前运行。从我对mocha的阅读中,这不应该发生,因为它在运行'it'块之前等待之前和之前的每个完成。但是,也许,getRequest的回调在它阻塞后排队?

    我做错了什么或误会了?

1 个答案:

答案 0 :(得分:1)

你肯定必须设置你的beforeEach挂钩,以便Mocha知道它是异步的:你要么向Mocha返回一个承诺,要么你使用done回调。要使用done回调,您必须声明一个需要done的回调:

beforeEach(function (done) {

(您可以随意调用它(例如finished),只要您保持一致(稍后再致电finished())。)