课程表

CoffeeScript 语法

CoffeeScript 类和对象

CoffeeScript 字符串

CoffeeScript 数组

CoffeeScript 日期和时间

CoffeeScript 数学

CoffeeScript 方法

CoffeeScript 元编程

CoffeeScript jQuery

CoffeeScript 正则表达式

CoffeeScript 网络

CoffeeScript 设计模式

CoffeeScript 数据库

CoffeeScript 测试

工具箱
速查手册

使用 Nodeunit 测试

当前位置:免费教程 » JS/JS库/框架 » CoffeeScript

问题

假如你正在使用 CoffeeScript 并且想要验证功能是否与预期一致,便可以决定使用 Nodeunit 测试框架。

讨论

Nodeunit 是一种 JavaScript 对于单元测试库( Unit Testing libraries )中 xUnit 族的实现,Java, Python, Ruby, Smalltalk 中均可以使用。

当使用 xUnit 族测试框架时,你需要将所需测试的描述预期功能的代码写在一个文件中。

例如,我们希望我们的计算器可以进行加法和减法,并且对于正负数均可以正确计算,我们的测试如下。

  1. # test/calculator.test.coffee
  2. Calculator = require '../calculator'
  3. exports.CalculatorTest =
  4. 'test can add two positive numbers': (test) ->
  5. calculator = new Calculator
  6. result = calculator.add 2, 3
  7. test.equal(result, 5)
  8. test.done()
  9. 'test can handle negative number addition': (test) ->
  10. calculator = new Calculator
  11. result = calculator.add -10, 5
  12. test.equal(result, -5)
  13. test.done()
  14. 'test can subtract two positive numbers': (test) ->
  15. calculator = new Calculator
  16. result = calculator.subtract 10, 6
  17. test.equal(result, 4)
  18. test.done()
  19. 'test can handle negative number subtraction': (test) ->
  20. calculator = new Calculator
  21. result = calculator.subtract 4, -6
  22. test.equal(result, 10)
  23. test.done()

安装 Nodeunit

在可以运行你的测试之前,你必须先安装 Nodeunit :

首先创建一个 package.json 文件

  1. {
  2. "name": "calculator",
  3. "version": "0.0.1",
  4. "scripts": {
  5. "test": "./node_modules/.bin/nodeunit test"
  6. },
  7. "dependencies": {
  8. "coffee-script": "~1.4.0",
  9. "nodeunit": "~0.7.4"
  10. }
  11. }

接下来从一个终端运行。

  1. $ npm install

运行测试

使用代码行可以简便地运行测试文件:

  1. $ npm test

测试失败,因为我们并没有 calculator.coffee

  1. suki@Yuzuki:nodeunit_testing (master)$ npm test
  2. npm WARN package.json calculator@0.0.1 No README.md file found!
  3. > calculator@0.0.1 test /Users/suki/tmp/nodeunit_testing
  4. > ./node_modules/.bin/nodeunit test
  5. /Users/suki/tmp/nodeunit_testing/node_modules/nodeunit/lib/nodeunit.js:72
  6. if (err) throw err;
  7. ^
  8. Error: ENOENT, stat '/Users/suki/tmp/nodeunit_testing/test'
  9. npm ERR! Test failed. See above for more details.
  10. npm ERR! not ok code 0

我们创建一个简单文件

  1. # calculator.coffee
  2. class Calculator
  3. module.exports = Calculator

并且重新运行测试套件。

  1. suki@Yuzuki:nodeunit_testing (master)$ npm test
  2. npm WARN package.json calculator@0.0.1 No README.md file found!
  3. > calculator@0.0.1 test /Users/suki/tmp/nodeunit_testing
  4. > ./node_modules/.bin/nodeunit test
  5. calculator.test
  6. CalculatorTest - test can add two positive numbers
  7. TypeError: Object #<Calculator> has no method 'add'
  8. ...
  9. CalculatorTest - test can handle negative number addition
  10. TypeError: Object #<Calculator> has no method 'add'
  11. ...
  12. CalculatorTest - test can subtract two positive numbers
  13. TypeError: Object #<Calculator> has no method 'subtract'
  14. ...
  15. CalculatorTest - test can handle negative number subtraction
  16. TypeError: Object #<Calculator> has no method 'subtract'
  17. ...
  18. FAILURES: 4/4 assertions failed (31ms)
  19. npm ERR! Test failed. See above for more details.
  20. npm ERR! not ok code 0

通过测试

让我们对方法进行实现来观察测试是否可以通过。

  1. # calculator.coffee
  2. class Calculator
  3. add: (a, b) ->
  4. a + b
  5. subtract: (a, b) ->
  6. a - b
  7. module.exports = Calculator

当我们重新运行测试时可以看到全部通过:

  1. suki@Yuzuki:nodeunit_testing (master)$ npm test
  2. npm WARN package.json calculator@0.0.1 No README.md file found!
  3. > calculator@0.0.1 test /Users/suki/tmp/nodeunit_testing
  4. > ./node_modules/.bin/nodeunit test
  5. calculator.test
  6. CalculatorTest - test can add two positive numbers
  7. CalculatorTest - test can handle negative number addition
  8. CalculatorTest - test can subtract two positive numbers
  9. CalculatorTest - test can handle negative number subtraction
  10. OK: 4 assertions (27ms)

重构测试

既然测试全部通过,我们应看一看我们的代码或测试是否可以被重构。

在我们的测试文件中,每个测试都创建了自己的 calculator 实例。这会使我们的测试相当的重复,特别是对于大型的测试套件。理想情况下,我们应该考虑将初始化代码移动到每次测试之前运行。

通常在其他的 xUnit 库中,Nodeunit 会提供一个 setUp(以及 tearDown )功能会在测试前调用。

  1. Calculator = require '../calculator'
  2. exports.CalculatorTest =
  3. setUp: (callback) ->
  4. @calculator = new Calculator
  5. callback()
  6. 'test can add two positive numbers': (test) ->
  7. result = @calculator.add 2, 3
  8. test.equal(result, 5)
  9. test.done()
  10. 'test can handle negative number addition': (test) ->
  11. result = @calculator.add -10, 5
  12. test.equal(result, -5)
  13. test.done()
  14. 'test can subtract two positive numbers': (test) ->
  15. result = @calculator.subtract 10, 6
  16. test.equal(result, 4)
  17. test.done()
  18. 'test can handle negative number subtraction': (test) ->
  19. result = @calculator.subtract 4, -6
  20. test.equal(result, 10)
  21. test.done()

我们可以重新运行测试,仍然可以全部通过。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号