문제

I'm writing a simple REST service in Node.js (just experimenting), trying to figure out if Node has matured enough yet. I'm also using NodeUnit for my unit testing.

Now, NodeUnit works fine as a testing framework for testing GET-requests, using the HttpUtils, however, testing POST-requests doesn't seem to be obvious.

Testing GET looks like this:

exports.testHelloWorld = function(test) {
    test.expect(1);
    httputil(app.cgi(), function(server, client) {
        client.fetch('GET', '/', {}, function (resp) {
            test.equals('hello world'), resp.body);
            test.done();
        });
    });
}

But how do I test POST-requests? I can change 'GET' to 'POST' and try to write something to 'client', however this doesn't work before .fetch is called because there's no connection yet. And it doesn't work in the .fetch callback function either, because at that time the request has already been executed.

I've looked into the nodeunit code, and there doesn't seem to be support for POSTing data at the moment. So here's my questions:

  • What does it take to test POST-requests?
  • Should I even test POST-requests in a unit test, or does that fall under an integration test and I should use another approach?
도움이 되었습니까?

해결책

You could try this library instead of nodeunit: https://github.com/masylum/testosterone

It's built specifically to test web apps over http.

다른 팁

I've just written this library for testing HTTP servers with nodeunit:

https://github.com/powmedia/nodeunit-httpclient

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top