当前位置:K88软件开发文章中心编程语言JavaScriptCoffeeScript → 文章内容

CoffeeScript 使用 Nodeunit 测试

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 15:34:29

由 珍珍阿姨 创建,Carrie 最后一次修改 2016-08-12 使用 Nodeunit 测试问题假如你正在使用CoffeeScript并且想要验证功能是否与预期一致,便可以决定使用Nodeunit测试框架。讨论Nodeunit是一种JavaScript对于单元测试库( Unit Testing libraries )中xUnit族的实现,Java, Python, Ruby, Smalltalk中均可以使用。当使用xUnit族测试框架时,你需要将所需测试的描述预期功能的代码写在一个文件中。例如,我们希望我们的计算器可以进行加法和减法,并且对于正负数均可以正确计算,我们的测试如下。# test/calculator.test.coffeeCalculator = require '../calculator'exports.CalculatorTest = 'test can add two positive numbers': (test) -> calculator = new Calculator result = calculator.add 2, 3 test.equal(result, 5) test.done() 'test can handle negative number addition': (test) -> calculator = new Calculator result = calculator.add -10, 5 test.equal(result, -5) test.done() 'test can subtract two positive numbers': (test) -> calculator = new Calculator result = calculator.subtract 10, 6 test.equal(result, 4) test.done() 'test can handle negative number subtraction': (test) -> calculator = new Calculator result = calculator.subtract 4, -6 test.equal(result, 10) test.done()安装 Nodeunit在可以运行你的测试之前,你必须先安装Nodeunit:首先创建一个package.json文件{ "name": "calculator", "version": "0.0.1", "scripts": { "test": "./node_modules/.bin/nodeunit test" }, "dependencies": { "coffee-script": "~1.4.0", "nodeunit": "~0.7.4" }}接下来从一个终端运行。$ npm install运行测试使用代码行可以简便地运行测试文件:$ npm test测试失败,因为我们并没有calculator.coffeesuki@Yuzuki:nodeunit_testing (master)$ npm testnpm WARN package.json calculator@0.0.1 No README.md file found!> calculator@0.0.1 test /Users/suki/tmp/nodeunit_testing> ./node_modules/.bin/nodeunit test/Users/suki/tmp/nodeunit_testing/node_modules/nodeunit/lib/nodeunit.js:72 if (err) throw err; ^Error: ENOENT, stat '/Users/suki/tmp/nodeunit_testing/test'npm ERR! Test failed. See above for more details.npm ERR! not ok code 0我们创建一个简单文件# calculator.coffeeclass Calculatormodule.exports = Calculator并且重新运行测试套件。suki@Yuzuki:nodeunit_testing (master)$ npm testnpm WARN package.json calculator@0.0.1 No README.md file found!> calculator@0.0.1 test /Users/suki/tmp/nodeunit_testing> ./node_modules/.bin/nodeunit testcalculator.test? CalculatorTest - test can add two positive numbersTypeError: Object #<Calculator> has no method 'add' ...? CalculatorTest - test can handle negative number additionTypeError: Object #<Calculator> has no method 'add' ...? CalculatorTest - test can subtract two positive numbersTypeError: Object #<Calculator> has no method 'subtract' ...? CalculatorTest - test can handle negative number subtractionTypeError: Object #<Calculator> has no method 'subtract' ...FAILURES: 4/4 assertions failed (31ms)npm ERR! Test failed. See above for more details.npm ERR! not ok code 0通过测试让我们对方法进行实现来观察测试是否可以通过。# calculator.coffeeclass Calculator add: (a, b) -> a + b subtract: (a, b) -> a - bmodule.exports = Calculator当我们重新运行测试时可以看到全部通过:suki@Yuzuki:nodeunit_testing (master)$ npm testnpm WARN package.json calculator@0.0.1 No README.md file found!> calculator@0.0.1 test /Users/suki/tmp/nodeunit_testing> ./node_modules/.bin/nodeunit testcalculator.test? CalculatorTest - test can add two positive numbers? CalculatorTest - test can handle negative number addition? CalculatorTest - test can subtract two positive numbers? CalculatorTest - test can handle negative number subtractionOK: 4 assertions (27ms)重构测试既然测试全部通过,我们应看一看我们的代码或测试是否可以被重构。在我们的测试文件中,每个测试都创建了自己的calculator实例。这会使我们的测试相当的重复,特别是对于大型的测试套件。理想情况下,我们应该考虑将初始化代码移动到每次测试之前运行。通常在其他的xUnit库中,Nodeunit会提供一个setUp(以及tearDown)功能会在测试前调用。Calculator = require '../calculator'exports.CalculatorTest = setUp: (callback) -> @calculator = new Calculator callback() 'test can add two positive numbers': (test) -> result = @calculator.add 2, 3 test.equal(result, 5) test.done() 'test can handle negative number addition': (test) -> result = @calculator.add -10, 5 test.equal(result, -5) test.done() 'test can subtract two positive numbers': (test) -> result = @calculator.subtract 10, 6 test.equal(result, 4) test.done() 'test can handle negative number subtraction': (test) -> result = @calculator.subtract 4, -6 test.equal(result, 10) test.done()我们可以重新运行测试,仍然可以全部通过。

CoffeeScript 使用 Nodeunit 测试