在使用Node.js(CompoundJS + PassportJS)構建的應用程序中,如何在鎖定的控制器上運行Mocha測試並要求登錄?我嘗試過使用Superagent,但沒有太多的運氣,並且使用它需要運行服務器來運行測試。我已經非常接近這種方法,但不想讓服務器運行來運行單元測試。如何使用Node.js和Passport設置Mocha測試
我也試過包括護照和使用request.login
的方法,我最終在一個點,我不斷收到錯誤passport.initialize()
中間件未被使用。
我試圖堅持生成的CompoundJS測試,這些測試工作很好,直到涉及身份驗證。默認CompoundJS測試運行init.js
文件,該文件將是不錯的處理身份驗證並以某種方式提供給每個控制器測試:
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;
};
控制器/ 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();
});
});
});
這些都失敗與AssertionError: expected 403 to equal 200
或AssertionError: expected false to be true
由於得到一個錯誤時,第一控制器的測試運行:{[錯誤:連接ECONNREFUSED]代碼:「ECONNREFUSED」,錯誤號:「ECONNREFUSED」,系統調用: 'connect'} – Michael
確保使用'init.js'運行mocha:'mocha test/init.js test/controllers/users_controller.test.js' – absynce
另外,驗證prepApp函數是否正在使用'console.log ('prepApp');'prepApp'頂部的''行。 – absynce