在Travis上获得随机结果

时间:2016-08-02 19:44:58

标签: node.js express mocha travis-ci supertest

我的测试给出了Travis的随机结果。以下是我在Travis上测试失败的日志:

https://travis-ci.org/superzaky/node-portfolio-zaky/builds/149718369

以下是我的测试确实传递给Travis的日志:

https://travis-ci.org/superzaky/node-portfolio-zaky/builds/149560005

两个分支都包含完全相同的代码。

在我的计算机上,我还可以将测试传递给屏幕截图:http://i.imgur.com/zXOINsD.png

我还在笔记本电脑上克隆了我的存储库。测试也通过了......我怀疑它与我写的方式有关 我在100-Login.js中的测试:

require('../utils');
require('events').EventEmitter.prototype._maxListeners = 100;

var supertest = require("supertest");
var should = require("should");
var assert = require('chai').assert;
var app = require('../../app');
// This agent refers to PORT where our program is running.
var server = supertest.agent(app);
// UNIT test begin

describe("A user logs in", function () {
    it('should create a SINGLE session on /api/auth/login POST', function (done) {
        //calling LOGIN api
        server
                .post('/api/auth/login')
                .send({
                    username: "jimmy",
                    password: "open"
                })
                .expect("Content-type", /json/)
                .expect(200)
                .end(function (err, res) {
                    var data = {
                        _id: "000000000000000000000001",
                        name: "Jimmy Doe",
                        username: "jimmy",
                        admin: false
                    };
                    res.status.should.equal(200);
                    assert.deepEqual(res.body, data);
                    done();
                });

    });

    it('should display a SINGLE session on /api/auth/ GET', function (done) {
        //We check if a session is created by sending a GET request to /api/auth
        server
                .get('/api/auth/')
                .expect("Content-type", /json/)
                .expect(200)
                .end(function (err, res) {
                    var data = {
                        _id: "000000000000000000000001",
                        name: "Jimmy Doe",
                        username: "jimmy",
                        admin: false
                    };
                    res.status.should.equal(200);
                    assert.deepEqual(res.body, data);
                    done();
                });
    });

    it('should delete a SINGLE session on /api/auth/logout GET', function (done) {
        //We check if a session is created by sending a GET request to /api/auth
        server
                .get('/api/auth/logout')
                .expect("Content-type", /json/)
                .expect(200)
                .end(function (err, res) {   
                    var data = "Successfully logged out";
                    res.status.should.equal(200);
                    assert.deepEqual(res.body, data);
                    done();
                });
    });

    it('should NOT display a SINGLE session on /api/auth/ GET', function (done) {
        //We check if a session is created by sending a GET request to /api/auth
        server
                .get('/api/auth/')
                .expect("Content-type", /json/)
                .expect(404)
                .end(function (err, res) {
                    var data = "Session not found";
                    res.status.should.equal(404);
                    assert.deepEqual(res.body, data);
                    done();
                });
    });
});

require('events').EventEmitter.prototype._maxListeners = 0;

以下是使测试通过链接的实际代码:https://github.com/superzaky/node-portfolio-zaky/blob/travis/controllers/AuthController.js

但这对我来说很好看。任何人都可能知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您应该检查本地数据库中的内容。

根据您最初使用username = john注册用户的其他测试(请参阅this test,第20行),将不会有username = jimmy的用户,这是您使用的在测试失败的情况下POST到/api/auth/login时的用户名凭据(参见this test,第18行)。

传递本地计算机的测试可能会通过,因为您的本地数据库中也有一个名为“jimmy”的用户。

至于为什么第二次测试失败,它失败了,因为会话创建测试在它之前就失败了,并且期望“jimmy”存在一个会话。