如何使用Mocha.js和Should.js有效地测试我的express.js应用程序是否发送回正确的数据?
我这样问的原因是,有时,如果使用req.xhr,我们会根据路由的扩展名以不同的格式发回相同的数据。因此,出于这个原因,我们希望确保路由在给定的情况下执行正确的条件。
有没有办法让express发回一个should.js可以读取的结果?
发布于 2013-07-23 11:58:49
您可以使用mocha和request进行端到端的HTTP测试。下面是一个示例,其中app.js运行服务器:
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; // Avoids DEPTH_ZERO_SELF_SIGNED_CERT error for self-signed certs
var request = require('request').defaults({ encoding: null });
var fs = require('fs');
var expect = require('expect.js');
var app = require("../../app.js");
var hash = function (file) { crypto.createHash('sha1').update(file).digest('hex') };
describe('Static tests', function () {
it('should serve out the correct favicon', function (done) {
var favicon = fs.readFileSync(__dirname + '/../../../public/img/favicon.ico');
request.get('https://localhost:' + process.env.PORT + '/favicon.ico', function (err, res, body) {
expect(res.statusCode).to.be(200);
expect(hash(body)).to.be(hash(favicon));
done();
});
});
});https://stackoverflow.com/questions/17651743
复制相似问题