单元测试/测试文件中是否存在功能(Unit Testing / Test if a function is present within a file)

编程入门 行业动态 更新时间:2024-10-27 11:17:47
单元测试/测试文件中是否存在功能(Unit Testing / Test if a function is present within a file)

我想知道是否有一种方法使用mocha和chai来测试代码是否存在整个代码,例如像这样的函数。

myfunction(arg1) { ...... }

如果代码存在,则测试应返回true。

PD:我知道对整个代码进行测试可能看起来很讨厌,但有时在进行培训时,学生会编写很多代码,如果某些内容不起作用,我们应该检查他们已经完成的整个步骤,避免我想写一个测试来寻找某些代码片段。 我已经编写了其他测试来检查参数是否存在,数组元素长度等。

提前致谢。

I would like to know if there is a way using mocha and chai to test the code for the existence of a whole piece of code, like for instance a function like this.

myfunction(arg1) { ...... }

If the code is present, then the test should return true.

PD: I know that testing for an entire piece of code may look like a nasty thing, but sometimes when conducting a training the students code a lot, and if something is not working we should review the entire set of steps they've done, to avoid that I want to write a test to look for certain pieces of code. I already wrote other tests to check for the existence of arguments, array element length, etc.

Thanks in advance.

最满意答案

有各种方法可以用chai来做到这一点。

如果您正在测试的函数位于global ( window )命名空间,则可以使用以下命令:

expect(myfunction).to.exist // asserts target is neither null or undefined`

要么

expect(myfunction).to.be.ok // asserts target is truthy

请参阅这个方便的备忘单,了解mocha / chai断言/期望。

There are various ways you can do this with chai.

If the function you are testing is on the global (window) namespace, you can use the following:

expect(myfunction).to.exist // asserts target is neither null or undefined`

or

expect(myfunction).to.be.ok // asserts target is truthy

Refer to this handy cheat sheet for mocha/chai assertions/expectations.

更多推荐

本文发布于:2023-08-02 16:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1378299.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是否存在   单元测试   功能   文件   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!