循环摩卡测试? [英] Loop Mocha tests?

查看:86
本文介绍了循环摩卡测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试循环一个mocha测试套件(我想针对具有预期结果的大量值来测试我的系统),但是我无法使其正常工作.例如:

I'm trying to loop a mocha test suite (I want to test my system against a myriad of values with expected results), but I can't get it to work. For example:

spec/example_spec.coffee :

test_values = ["one", "two", "three"]

for value in test_values
  describe "TestSuite", ->
    it "does some test", ->
      console.log value
      true.should.be.ok

问题是我的控制台日志输出如下:

The problem is that my console log output looks like this:

three
three
three

我希望它看起来像这样:

Where I want it to look like this:

one
two
three

如何在我的摩卡咖啡测试中遍历这些值?

How can I loop over these values for my mocha tests?

推荐答案

此处的问题是您要关闭"value"变量,因此它将始终求值为最后一个值.

The issue here is that you're closing over the "value" variable, and so it will always evaluate to whatever its last value is.

类似的事情会起作用:

test_values = ["one", "two", "three"]
for value in test_values
  do (value) ->
    describe "TestSuite", ->
      it "does some test", ->
        console.log value
        true.should.be.ok

之所以可行,是因为将值传递给该匿名函数时,它会被复制到外部函数中的新value参数,因此不会被循环更改.

This works because when value is passed into this anonymous function, it is copied to the new value parameter in the outer function, and is therefore not changed by the loop.

添加了咖啡脚本很好".

Added coffeescript "do" niceness.

这篇关于循环摩卡测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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