문제

Node.js 및 NodeUnit으로 속도를 높이려고하지만 NodeUnit에서 호출을 보지 못하는 문제를 찾고 있습니다. test.done() 테스트 중 하나에서.

코드:

// Added for clarity.
var client = require("restify").createJsonClient({
    "version": "*",
    "url": "http://localhost:" + server.Port
});

exports["tests"] = {
    "setUp": function (callback) {
        server.StartServer();
        callback();
    },
    "tearDown": function (callback) {
        callback();
    },
    "CanIHaveSomeTeaPlease?": function (test) {
        test.expect(4);
        client.get("/tea", function (err, req, res, data) {
            test.equal(err.statusCode, 418, "Expected ImATeapot Error.");
            test.equal(err.message, "Want a biscuit?", "Expected to be asked if I want a buscuit.");
            test.equal(err.restCode, "ImATeapotError");
            test.equal(err.name, "ImATeapotError");
            test.done();
        });
    },

    // Note: I expect this test to fail as it is a copy of the above
    //       test on a different url that doesn't return the ImATeapot
    //       HTTP error. But it doesn't look like it's detecting it
    //       properly.

    "TakeThisInfo": function (test) {
        test.expect(4);
        client.put("/push", {
            "hello": "world"
        }, function (err, req, res, data) {
            test.equal(err.statusCode, 418, "Expected ImATeapot Error.");
            test.equal(err.message, "Want a biscuit?", "Expected to be asked if I want a buscuit.");
            test.equal(err.restCode, "ImATeapotError");
            test.equal(err.name, "ImATeapotError");
            test.done();
        });
    }
};

산출:

FAILURES: Undone tests (or their setups/teardowns):
- tests - TakeThisInfo

To fix this, make sure all tests call test.done()

나는 그것이 어리석은 일을 바라고 있습니다.

버전 :-

Node: 0.10.21
NPM: 1.3.11
Nodeunit: 0.8.2
Grunt-CLI: 0.1.10
Grunt: 0.4.1
도움이 되었습니까?

해결책

첫째, 코드에 "서버"가 무엇인지 모르겠지만, 비동기식이 될 것으로 예상되므로 설정 기능에서 이와 비슷한 것을 갖기를 기대할 것입니다.

function (callback) {
  server.StartServer(function(){
    callback();
  });
}

둘째, 그 노드 유닛을 유지하십시오 테스트 후 및마다 시작 및 파열 기능을 실행합니다. 그래서 나는 당신이 당신의 서버를 2 번 시작하고 있다고 생각합니다 (분해에서는 실제로 닫히지 않습니다).

다른 팁

나는 지난 몇 시간 동안이 문제를 엉망으로 만들었고, NodeUnit은 나중에 IO 또는 Settimeout 유형 프로세스에 의해 트리거되는 함수에서 예외를 포착하고 표시 할 수있는 능력이 없다는 것입니다. JavaScript가 실행되는 방식을 고려할 때 이것은 놀라운 일이 아닙니다. 예외가 없다고 확신하면 모든 것이 작동하지만 코드에 오류가 발생하면 "취소 테스트"메시지가 표시되고 다른 것은 없습니다. 다음은 내 문제를 해결하기 위해 한 일입니다 (예로서 Restify Route 사용) :

function myRoute(req, res, next) {
    try {
        // your code goes here...
    }
    catch (err) {
        // write it to the console (for unit testing)
        console.log(err);
        // pass the error to the next function.
        next(err);
    }
}

이런 식으로 문제를 이해하면 훨씬 더 명확하고 모든 테스트를 통과 할 수 있었기 때문에 문제를 해결했습니다!

나는 당신이 실제로 전화하지 않는다고 생각합니다 test.done() 두 번째 테스트에서. a console.log() 실제로 전화를 걸어 실제로 전화를 걸고 있는지 확인하십시오.

fwiw, 나는 아래의 단순화 된 버전의 테스트 버전을 사용하여 설명 된 문제를 다시보고했다. 당신이 생략하면 on('error', function() {...}) 핸들러, 두 번째 테스트가 완료되지 않습니다. 따라서 나의 이론은 당신의 이론입니다 /push 엔드 포인트는 Restify 모듈에서 다른 동작을 트리거합니다. 즉, 당신입니다 확실한 Restify는 콜백을 호출합니다 err 거기에 재산이 있습니까, 아니면 다른 일을하고 있습니까? ... 예를 들어, 같은 이벤트를 방출합니다. http.get 아래에서.

var http = require('http');

exports.test1 = function (test) {
  test.expect(1);
  http.get({hostname: "www.broofa.com", path: "/"}, function (res) {
    test.equal(res.statusCode, 200, 'got 200');
    test.done();
  });
};

exports.test2 = function (test) {
  test.expect(1);
  http.get({hostname: "www.no-such-domain.com", path: "/"}, function (res) {
    test.equal(res.statusCode, 200, 'got 200');
    test.done();
  }).on('error', function() {
    // Comment line below out to repro the "Undone tests" error
    test.done();
  });
};

나는 서버를 설정에서 자체 프로세스로 포크 한 다음 분해에서 죽여서 그것을 해결하고 있습니다. 문제는 서버가 생성되고 종료되지 않는 것과 관련이 있다고 생각합니다. 감사합니다 @matteofigus.

var cp = null; // child process
exports["tests"] = {
    "setUp": function (callback) {
        cp = fork("./lib/server.js", {"silent": true});
        callback();
    },
    "tearDown": function (callback) {
        cp.kill("SIGHUP");
        callback();
    },
    "CanIHaveSomeTeaPlease?": function (test) {
        test.expect(4);
        client.get("/tea", function (err, req, res, data) {
            test.equal(err.statusCode, 418, "Expected ImATeapot Error.");
            test.equal(err.message, "Want a biscuit?", "Expected to be asked if I want a buscuit.");
            test.equal(err.restCode, "ImATeapotError");
            test.equal(err.name, "ImATeapotError");
            test.done();
        });
    },
    "TakeThisInfo": function (test) {
        test.expect(1);
        client.put("/push", {
            "hello": "world"
        }, function (err, req, res, data) {
            test.ok(false);
            test.done();
        });
    }
};
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top