使用Node.js中的http.get库与誓言进行异步测试 [英] Async testing with vows using the http.get library in Node.js

查看:153
本文介绍了使用Node.js中的http.get库与誓言进行异步测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段时间试图让基本的http测试与誓言一起工作。

I'm having a doozie of a time trying to get a basic http test to work with vows.

我想我已经遵循了誓言中的异步示例 http://vowsjs.org/#-writing-asynchronous-tests 并替换相应的电话,但我必须遗漏一些东西。

I think I've followed the async example from vows http://vowsjs.org/#-writing-asynchronous-tests and substitued the appropriate calls, but I must be missing something.

测试代码如下所示:

var http = require('http'),
    vows = require('vows'),
    assert = require('assert');

vows.describe("homepage").addBatch({
  "Get the home page": {
    topic: function() {
      http.get({'host': "127.0.0.1", 'port': 5000, 'path': '/'}, this.callback);
    },
    'should respond with 200 OK': function(res) {
      assert.equal(res.statusCode, 200);
    }
  }
}).export(module);

当我尝试为此运行测试时出现以下错误:

I get the following error when I try to run the test for this:

/Users/<home_folder>/node_modules/vows/lib/vows.js:80
rrored', { type: 'promise', error: err.stack || err.message || JSON.stringify(
                                                                    ^
TypeError: Converting circular structure to JSON
    at Object.stringify (native)
    at EventEmitter.<anonymous> (/Users/<home_folder>/node_modules/vows/lib/vows.js:80:90)
    at EventEmitter.emit (events.js:64:17)
    at /Users/<home_folder>/node_modules/vows/lib/vows/context.js:31:52
    at ClientRequest.<anonymous> (/Users/<home_folder>/node_modules/vows/lib/vows/context.js:46:29)
    at ClientRequest.g (events.js:143:14)
    at ClientRequest.emit (events.js:64:17)
    at HTTPParser.onIncoming (http.js:1349:9)
    at HTTPParser.onHeadersComplete (http.js:108:31)
    at Socket.ondata (http.js:1226:22)

我可以得到一个简单的http示例来处理它自己。我可以得到誓言的例子来处理它自己但我不能因为某种原因将它们结合起来。我真的很感激所以我在这帮忙。我一直试图让它工作一段时间(包括大量谷歌搜索)。

I can get a simple http example to work on it's own. I can get the vows example to work on it's own but I can't combine them for whatever reason. I'd really appreciate some help here. I've been trying to get this to work for a while now (including much googling).

更新:

显然在回调中添加错误参数解决了这个问题,感谢Alexis Sellier(誓言的创造者)的帮助。

Apparently adding an error argument to the call back solves this problem, thanks to help from Alexis Sellier (creator of vows).

但是我不知道为什么。当写出自己的http lib示例时,不需要错误参数。我无法在誓言中找到任何文件来说明为什么需要它,所以我有点亏。

But I have no idea why. When writing out the http lib example on it's own no error argument is required. I can't find any documentation in vows to indicate why it's needed so I'm at a bit of a loss.

我的新问题是为什么需要错误参数在誓言中使用http lib时?

My new question is why is the error argument required when using the http lib in vows?

推荐答案

检查誓言的源代码后,我想我知道原因。 Vows总是确保当你调用 this.callback 时,生成的接收函数的第一个参数始终是一个错误对象。誓言通过以下规则解释回调:

After checking vow's source code, I think I know why. Vows always ensure that when you call this.callback, the resulting receiver function's first argument is always an error object. Vows interpret the callbacks by these rules:


  1. 如果原始回调的第一个参数是布尔值,请使用它来确定是否或者不将错误对象附加到接收回调(例如 path.exists(boolean)将发出回调(错误,存在)而不是)

如果第一个参数是一个对象,假设它是一个错误对象并使用它来确定是否将原始回调添加到错误或成功列表。这个列表存在的原因是支持基于承诺的测试我猜?

If the first argument is an object, assume it's an error object and use that to determine whether to add the originating callback to the "error" or "success" list. The reason this list exists is to support promise based tests I guess?

虽然我无法确认以上是正确的,我的经验是,誓言的异步样式是为了支持节点样式的回调(例如,作为第一个arg的错误),并且不符合此标准的第三方npm模块将难以测试。

While I can't confirm the above is correct, my experience is that vows' async style is made to support node-styled callbacks (e.g. err as the first arg), and 3rd party npm modules that don't conform to this standard will be hard to test.

请不要将我的答案视为福音,因为这是我自己的经验。另一个问题是当您在要测试的函数内部进行异步操作时 - 除非您提供回调,否则vows将无法正确处理它。

Please don't take my answer as gospel, as this is my own experience. Another gotcha is when you have async operations inside the function that you want to test - unless you provide a callback, vows won't be able to handle it properly.

个人,我认为誓言仍然难以测试异步代码。我希望它有一些 waitFor()直到()流量控制功能。

Personally, I think vows still make it hard to test async code. I wish it had some waitFor() or until() flow control functions though.

我的建议?处理异步代码时,请使用步骤。不要让誓言控制你的流量。

My suggestion? When dealing with async code, use Step. Don't let vows control your flow.

这篇关于使用Node.js中的http.get库与誓言进行异步测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆