【发布时间】:2011-10-02 01:25:56
【问题描述】:
我是整个堆栈的新手——javascript、node.js、coffeescript、nodeunit。认为我应该一步一步做吗?你可能是对的,但我仍然不会这样做。
这里是测试文件:
testCase = require('nodeunit').testCase
Server = require('./web').WebServer
Client = require('../lib/client').Client
Request = require('../lib/request').Request
OPTIONS = {host: 'localhost', port: 8080, path: '/', method: 'GET'}
SERVER = new Server OPTIONS.port
CLIENT = new Client
REQUEST = new Request OPTIONS
SERVER.start() # asynchronous!
module.exports = testCase
setUp: (callback) ->
callback()
tearDown: (callback) ->
callback()
testResponseBodyIsCorrect: (test) ->
test.expect 1
process.nextTick ->
CLIENT.transmit REQUEST #asynchronous!
process.nextTick ->
test.equal REQUEST.body, /Ooga/
test.done()
在内部,它只是 http 库的一个包装器。我正在使用节点 0.4.11。 这实际上不起作用。这里有两个异步调用。如果我在咖啡 REPL 中手动执行此操作,它可以工作——但 nodeunit 比我快得多,所以我遇到了一些事情,为了聪明,我会称之为竞争条件。 咧嘴一笑
这里是“传输”的实现: Http = 需要'http'
exports.Client = class Client
transmit: (request, callback = null) ->
req = Http.request request.options, (res) ->
res.setEncoding 'utf8'
res.on 'data', (chunk) ->
request.appendToResponseBody chunk
req.end()
console.log "Request sent!"
在运行测试之前,我需要确保服务器已绑定到端口,并且我需要确保“.transmit”在执行断言之前已完成其内部回调以获取响应。
什么是干净的方式(或者至少是有效的方式)来做到这一点?
【问题讨论】:
标签: node.js coffeescript nodeunit