我想知道是否有一种方法使用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 truthyRefer to this handy cheat sheet for mocha/chai assertions/expectations.
更多推荐
发布评论