Jak skonfigurować testy Mocha za pomocą Node.js i Passport

W aplikacji zbudowanej z Node.js (CompoundJS + PassportJS), w jaki sposób testy Mocha mogą być uruchamiane na kontrolerach, które są zablokowane i wymagają logowania? Próbowałem użyć Superagenta, ale nie mam dużo szczęścia i aby go użyć, serwer musi być uruchomiony, aby uruchomić testy. Bardzo zbliżyłem się do tej metody, ale nie chcę mieć uruchomionego serwera, aby uruchomić testy jednostkowe.

Próbowałem również w tym paszportu i używaniarequest.login Metoda skończyła się w punkcie, w którym ciągle otrzymywałem błądpassport.initialize() oprogramowanie pośrednie nie jest używane.

Próbuję trzymać się wygenerowanych testów CompoundJS, które działały świetnie, aż do włączenia uwierzytelniania. Domyślne testy CompoundJS uruchamiająinit.js plik, który byłoby miło obsługiwać uwierzytelnianie i jakoś udostępnić każdy test kontrolera:

require('should');
global.getApp = function(done) {
    var app = require('compound').createServer()
    app.renderedViews = [];
    app.flashedMessages = {};

    // Monkeypatch app#render so that it exposes the rendered view files
    app._render = app.render;
    app.render = function(viewName, opts, fn) {
        app.renderedViews.push(viewName);

        // Deep-copy flash messages
        var flashes = opts.request.session.flash;
        for (var type in flashes) {
            app.flashedMessages[type] = [];
            for (var i in flashes[type]) {
                app.flashedMessages[type].push(flashes[type][i]);
            }
        }

        return app._render.apply(this, arguments);
    }

    // Check whether a view has been rendered
    app.didRender = function(viewRegex) {
        var didRender = false;
        app.renderedViews.forEach(function(renderedView) {
            if (renderedView.match(viewRegex)) {
                didRender = true;
            }
        });
        return didRender;
    }

    // Check whether a flash has been called
    app.didFlash = function(type) {
        return !!(app.flashedMessages[type]);
    }

    return app;
};

controllers / users_controller_test.js

var app,
    compound,
    request = require('supertest'),
    sinon = require('sinon');

/** 
 * TODO: User CREATION and EDITs should be tested, with PASSPORT
 * functionality.
 */

function UserStub() {
    return {
        displayName: '',
        email: ''
    };
}

describe('UserController', function() {
    beforeEach(function(done) {
        app = getApp();
        compound = app.compound;
        compound.on('ready', function() {
            done();
        });
    });

    /**
     * GET /users
     * Should render users/index.ejs
     */
    it('should render "index" template on GET /users', function(done) {
        request(app)
            .get('/users')
            .end(function(err, res) {
                res.statusCode.should.equal(200);
                app.didRender(/users\/index\.ejs$/i).should.be.true;
                done();
            });
    });

    /*
     * GET /users/:id
     * Should render users/index.ejs
     */
    it('should access User#find and render "show" template on GET /users/:id',
        function(done) {
            var User = app.models.User;

            // Mock User#find
            User.find = sinon.spy(function(id, callback) {
                callback(null, new User);
            });

            request(app)
                .get('/users/42')
                .end(function(err, res) {
                    res.statusCode.should.equal(200);
                    User.find.calledWith('42').should.be.true;
                    app.didRender(/users\/show\.ejs$/i).should.be.true;

                    done();
                });
        });
});

Wszystkie one zawodząAssertionError: expected 403 to equal 200 lubAssertionError: expected false to be true

questionAnswers(1)

yourAnswerToTheQuestion