使用Node.js,Express,SinonJS和BusterJS进行集成测试

时间:2014-02-12 16:12:04

标签: node.js express sinon buster.js

在使用实时测试服务器进行集成测试时,我遇到了存根方法问题。

describe("Group - Integration", function() {

before(function() {
    this.timeout = 1000 * 5;
    this.server = http.createServer(app).listen(app.get('port'));
});

after(function() {
    this.server.close();
});

describe("group/get", function() {

    it("returns 200 and an empty body when an invalid response type is used", function(done) {
        var deferred = when.defer();

        request.get("http://localhost:3000/api/v2/username/key/not-json/group/get", done(function(error, response, body) {
            buster.assert.equals(error, null);
            buster.assert.equals(response.statusCode, 200);
            buster.assert.equals(body, "");
            deferred.resolver.resolve();
        }));

        return deferred.promise;
    });

    it("returns 401 unauthorized if invalid credentials are provided", function(done) {
        var deferred = when.defer();

        var response_body = '{ "non_field_errors": [ "Invalid API credentials." ] }';
        var stub = sinon.stub(request, 'post').yields(null, {statusCode: 400}, response_body);

        request.get("http://localhost:3000/api/v2/username/key/json/group/get", done(function(error, response, body) {
            buster.assert.equals(error, null);
            buster.assert.equals(response.statusCode, 401);
            buster.assert.equals(body, "");
            stub.restore();
            deferred.resolver.resolve();
        }));

        return deferred.promise;

    });

    // itEventually("returns 401 unauthorized if the company has been disabled", function() {
    //     expect("a").toBe("b");
    // });

});

});

我在每个“它”之前运行服务器。但是当我存根request.post时,它不起作用。我怀疑这是因为我在服务器启动后将其存根,但是当我尝试在服务器启动之前存根它时它也不起作用。有没有一种在实时测试服务器上存根方法的好方法?

0 个答案:

没有答案