在规范`expect`中复制Node.js`assert`断言 [英] Duplicating Node.js `assert` assertions in spec `expect`

查看:162
本文介绍了在规范`expect`中复制Node.js`assert`断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与使用开发人员自定义检查的非声明代码相反,

As opposed to non-asserted code with custom developer-friendly checks,

class Some {
  constructor(arg) {
    if (Array.isArray(arg) && arg[0] === 'foo')
      this.foobar = arg.concat('bar').join('');
    else
      console.error('Bad Some constructor arg');
  }
}

当前测试的代码中挤满了带有合理意义message参数的节点assert断言:

currently tested code is heavily packed with Node assert assertions with reasonably meaningful message arguments:

class Some {
  constructor(arg) {
    assert.deepEqual(arg, ['foo'], 'Some constructor arg');
    this.foobar = arg.concat('bar').join('');
  }
}

断言在那里

  • 保持代码平整且可读
  • 针对调用堆栈的错误用法提供有意义的反馈
  • 防止函数执行,并且不会进一步传播错误
  • 抛出错误并将错误处理留给调用者

当前规格可能如下所示:

Current spec may look like that:

it('...', () => {
  let some = new Some(['foo']);
  expect(some).to...

它将通过-在规范中声明了合意的用法,在测试代码中声明了合意的用法.

And it will pass - desirable usage is asserted in spec, undesirable usage it asserted in tested code.

要部分重叠代码断言,它可能是偶数

To overlap code assertions in part it may be even

it('...', () => {
  const { AssertionError } = require('assert');
  let some = new Some(['foo']);
  expect(some).to...
  expect(() => new Some(['bar']).to.throw(AssertionError);

因此,我们基本上在这里假设已经用assert在代码本身中完成了一半的测试工作,并跳过了详细信息(to.not.throw和匹配的AssertionError消息).

So we basically assume here that half of testing job was already done in the code itself with assert and skip the details (to.not.throw and matching AssertionError messages).

上面的示例使用的是Mocha + Chai,但同样适用于茉莉花.

The example above uses Mocha+Chai, but the same thing applies to Jasmine.

  • 应将应用程序断言视为其他任何代码行,并使用规范断言(抛出不抛出AssertionError消息匹配)加倍,采取捷径有什么后果?

  • Should app assertions be treated as any other code lines and doubled with spec assertions (to throw, to not throw, AssertionError message matching), what are the consequences of taking a shortcut?

expect之外,测试覆盖率工具(伊斯坦布尔)是否可以考虑应用代码中的assert断言?

Can test coverage tools (Istanbul) take into account assert assertions in app code in addition to expect?

测试人员可能会因为它是应用程序而不是抛出错误的规范断言而感到困惑吗?

May test runners be confused by the fact that it was app, not spec assertion that threw an error?

成功地证明或驳斥断言断言"观点的成功开源JS项目的一些示例也可能会有所帮助.

Some examples of successful open-source JS projects that prove or refute 'assert assert assertions' point in practice could be also helpful.

推荐答案

应将应用程序断言与其他任何代码行一样对待,并用规范断言(以引发,不引发,AssertionError消息匹配)加倍,采用快捷方式会带来什么后果?

Should app assertions be treated as any other code lines and doubled with spec assertions (to throw, to not throw, AssertionError message matching), what are the consequences of taking a shortcut?

可以使用应用程序断言来通知开发人员对某段代码的错误使用,理想情况下,它们绝不会在生产中发生.如果确实发生了,那么它们就可以像标准Error一样,用一种工具来识别出哪里出错了.但是理想情况下,它们是开发过程中的第一道防线,只有在那时才发生. (因此这就是为什么在某些语言中您可以同时禁用运行时断言的原因之一)

App assertions are there to notify a developer of incorrect use of a certain piece of code, ideally they will never happen in production. If it does happen, then they serve as a tool to identity what went wrong tough, in the same way standard Error's do. But ideally, they are a first line of defense during development, and they should only happen then. ( hence this is one reason why in some languages you can disable assertions for run time all together )

如果您编写一些使用断言来确保输入参数得到验证的类,或者不会不一致地使用该程序,则可以肯定的是,将该逻辑置于单元测试中也很有意义.如果您要在某个时候更改断言,则可以测试您是否不会破坏其他人的代码.

If you write some class that uses assertions to make sure input parameters are validated or the program isn't being used inconsistently, then for sure it makes a lot of sense to put that logic under unit tests as well. If you're gonna change the assertions at some point, you wanna be able to test that you don't break other people's code.

除了期望之外,测试覆盖率工具(伊斯坦布尔)是否可以考虑应用代码中的断言?

Can test coverage tools (Istanbul) take into account assert assertions in app code in addition to expect?

是的.如果您设置了一个单元测试,该单元测试将触发一个断言,然后在chai中捕获它,那么该代码路径将显示在覆盖率报告中.除非我误解了您的问题,否则这与任何其他代码路径都没有什么不同.

Yes. If you set up a unit test that will trigger an assertion and then capture it in chai, then that code path will show up in your coverage report. This is no different then any other code path's unless I'm mis understanding your question.

测试人员可能会对应用程序这一事实感到困惑,而不是对引发错误的规范断言感到困惑吗?

May test runners be confused by the fact that it was app, not spec assertion that threw an error?

当您使用assert模块编写断言时,它将抛出AssertionError.这是assert模块中的AssertionError类. 当chai引发错误时,它也会引发AssertionError,但这将是来自chai模块的完全不同的类,它们仅共享名称. 因此,对于断言从何而来应该不会造成任何混淆.
通常,您不会在所有的try/catch构造中都捕获测试代码中的断言,因此这确实不是问题.

When you write Assertions using the assert module, it's gonna be throwing an AssertionError. This is the AssertionError Class from the assert module. When chai throws an error it will also throw an AssertionError, but this will be an entirely different class coming from the chai module, they only share the name. As such there shouldn't be any confusing as to where assertions are originating from.
Usually you aren't capturing assertions in test code at all trough try / catch constructs so this really shouldn't be a problem.

通常,断言测试的状态要比单元测试少得多.但是确实没有正式定义断言应该在何处开始以及单元测试应该从哪里开始. 更多阅读:

Usually assertions test much less complex states then unit test. But where assertions end and unit tests should start is indeed not formally defined. More reading:

https://softwareengineering.stackexchange.com/questions/18288/are-asserts-or-unit-tests更重要

这篇关于在规范`expect`中复制Node.js`assert`断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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