简体   繁体   English

如何使用 jest 确保我只使用两个 arguments?

[英]How do I use jest to make sure I am only using two arguments?

I'm challenging myself to create my own tests for problems in freeCodeCamp.我正在挑战自己,为 freeCodeCamp 中的问题创建自己的测试。 Currently stuck on the second requirement for 'Subtract One Number from Another with JavaScript'.目前坚持“使用 JavaScript 从另一个数字中减去一个数字”的第二个要求。 Here are the requirements.以下是要求。

var difference = 45 - 0;

//The variable difference should be equal to 12.
   
 //You should only subtract one number from 45.

How could I write my own test to make sure I am only using two arguments?我如何编写自己的测试以确保我只使用两个 arguments?

(freeCodeCamp problem can be found here: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/subtract-one-number-from-another-with-javascript ) (freeCodeCamp 问题可以在这里找到: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/subtract-one-number-from-another-with-javascript

If you were testing this as part of an application, you wouldn't necessarily be testing the expression itself but rather some function that contains this expression.如果您将其作为应用程序的一部分进行测试,则不一定要测试表达式本身,而是测试包含此表达式的 function。 To organize that code example in a testable way, you could write a function like this:要以可测试的方式组织该代码示例,您可以像这样编写 function:

 function getDifference(numberToSubtract) { const difference = 45 - numberToSubtract; return difference; }

Then you could use Jest to assert the return value is 12 and that you've only used a single argument like this:然后你可以使用 Jest 来断言返回值为 12,并且你只使用了一个这样的参数:

expect(getDifference(33)).toBe(12);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM