在 Vows.js 中,如何在经历异步回调后恢复到原始主题?

发布于 2024-12-07 17:58:20 字数 1050 浏览 3 评论 0原文

假设我有以下序列:

vows.describe('Example').addBatch({
   'An example' : {
      topic: new Example(),
      'with an async method' : function(example) {
         example.asyncMethod(this.callback);
      },
      'will do some magic' : function(err, result) {
         assert.equal(result.magical, true);
      },
      'and then we will be back on track' : function(example) {
         assert.isTrue(example.backOnTrack); 
      }
   }
}).run();

测试“然后我们将回到正轨”是否可以与之前的主题(示例)相匹配?

编辑

vows.describe('Example').addBatch({
   'An example' : {
      topic: function(example){ // <- where example is a parent topic
         example.asyncMethod(this.callback);
      },    
      'callback after an async method will do some magic' : function(err, result) {
         assert.equal(result.magical, true);
      },
      'and then we will be back on track' : function(example) {
         assert.isTrue(example.backOnTrack); 
      }
   }
}).run();

Say I have the following sequence:

vows.describe('Example').addBatch({
   'An example' : {
      topic: new Example(),
      'with an async method' : function(example) {
         example.asyncMethod(this.callback);
      },
      'will do some magic' : function(err, result) {
         assert.equal(result.magical, true);
      },
      'and then we will be back on track' : function(example) {
         assert.isTrue(example.backOnTrack); 
      }
   }
}).run();

Is the test "and then we will be back on track" possible to hit with the topic (Example) from before?

EDIT

vows.describe('Example').addBatch({
   'An example' : {
      topic: function(example){ // <- where example is a parent topic
         example.asyncMethod(this.callback);
      },    
      'callback after an async method will do some magic' : function(err, result) {
         assert.equal(result.magical, true);
      },
      'and then we will be back on track' : function(example) {
         assert.isTrue(example.backOnTrack); 
      }
   }
}).run();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守护在此方 2024-12-14 17:58:20

主题返回将发送给您所有誓言的主题。

在第一个誓言中,“使用异步方法” this.callback 未定义,因为回调仅在主题中定义。在第二个誓言中,参数是 example 而不是 err, result

如果您想进行任何异步测试,您需要为您的主题设置一个函数并使用 this。回调或从主题返回一个EventEmitter

编辑:

您遇到的问题是从誓言返回多个主题。这是不可能的。

最简单的解决方案是返回一个新主题,该主题是两个主题的数组。这感觉很黑客。

更好的解决方案是确保单独测试您的主题。基本上你的测试“副作用”已经成功发生。副作用不应该发生

A topic returns a topic that will be send to all your vows.

In that first vow "with an async method" this.callback is undefined because a callback is only defined in a topic. In the second vow the arguments are example not err, result

If you want to do any async testing you need to set a function for your topic and use this.callback or return an EventEmitter from the topic.

Edit:

The issue you have is returning multiple topics from vows. This is not possible.

The easiest solution is to return a new topic that is an array of two topics. This feels hackish.

The better solution is to make sure that you test your topics in isolation. Basically your testing that "side-effects" have successfully occurred. Side effects should never occur

海风掠过北极光 2024-12-14 17:58:20

据我了解, topic 无论是使用简单的 topic: Something 还是 topic: function(... 语法,都只执行一次,当誓言按顺序执行时,在第二个示例中,您将处理修改后的示例

。将两个测试拆分为子上下文并拥有两个 topic 实例:新的示例()。

As far as I understand it, topics whether using the simple topic: something or topic: function(... syntax, are executed only once. And as vows are executed in sequence, in your second example, you'll be dealing with the modified example.

I'd take the simple but kind of annoying route of splitting the two tests into sub-contexts and having two instances of topic: new Example().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文