testing - Mocha.js 和 Should.js 来测试express.js应用程序

标签 testing express mocha.js should.js

如何使用 Mocha.js 和 Should.js 有效测试我的express.js 应用程序是否正在发送正确的数据?

我问的原因是,有时,如果req.xhr我们发送回相同的数据,但以不同的格式,具体取决于路由的扩展名。因此,出于这个原因,我们希望确保路由在给定的情况下执行正确的条件。

有什么方法可以让我快速发送回可以被should.js读取的结果吗?

最佳答案

您可以使用 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();
        });
    });
});

关于testing - Mocha.js 和 Should.js 来测试express.js应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17651743/

相关文章:

asp.net - 如何开发、测试、版本、同步SQL Server 2000、2005、2008、2008R2数据库?

node.js - Node-sass 没有与 TestCafe Docker Image 中使用的 Node 版本绑定(bind)

javascript - 处理并保持 node.js/express session

javascript - 解释connect session中的length和clear session store方法

javascript - 使用 sinon 和 mocha 执行测试时不执行 stub 函数

node.js - Mocha JS : How to share assertion/"it(' should')"code between tests

objective-c - 在 iOS 上测试 Mantle 模型时出错

javascript - Sequelize 关联模型查询

intellij-idea - Webstorm 可以和 Chai 语言链一起玩吗?

testing - XPath 2.0 在线测试器?