Async testing with vows using the http.get library in Node.js

前端 未结 2 505
迷失自我
迷失自我 2021-01-14 20:30

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

I think I\'ve followed the async example from vows http://vowsjs.org/#-writing-asyn

相关标签:
2条回答
  • 2021-01-14 21:16

    It is actually missing in the documentations which is still a bit short. But you can get a glimpse of it here in this page :

    'when peeled *asynchronously*': {
            topic: function (banana) {
                banana.peel(this.callback);
            },
            'results in a `PeeledBanana`': function (err, result) {
                assert.instanceOf (result, PeeledBanana);
            }
        }
    

    As it was said by Morten Siebuhr and Ruben Tan, this is how vows works and that is why it works like that.

    0 讨论(0)
  • 2021-01-14 21:17

    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. If the first argument of your originating callback is a boolean, use that to determine whether or not to append an error object to the receiving callback (e.g. path.exists(boolean) will emit callback(error, exists) instead)

    2. 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?

    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.

    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.

    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.

    0 讨论(0)
提交回复
热议问题