Testframework
The ideal Test Driven Development process will be:
- (setup/teardown env&mockups)
- write tests
- write code
- automation build & test process
You can write plain mockup, stubs...etc,
or use sinon to ease those tasks.
Read public/test/sinon_test.js
or slide for more example usage.
To be done
Spy: check is called
var callback = sinon.spy();
.....
assert(callback.Called)
To be done
There are two perspectives to do the test.
You can choose to follow the rules based on your development type (on browser or server-side):
-
files need to be named with
test_
prefix -
(browser test compatibility) make sure include the
test_
file and the target file for test inpublic/test/html
-
(server-side test compatibility) make sure you add this section in each test file:
if ('undefined' != typeof require) { // Require server-side-specific modules var chai = require('chai'); var assert = chai.assert; }
Thus can fetch required resource in server-side.
Drag and drop public/test/unittest/index.html into browser, and see the test report.
Run command
$ grunt mocha_phantomjs
To do the auto browser unit tests.
The client side unit test will be triggered while running grunt pack
or grunt static
command.
The server side unit test will be triggered while running default grunt command. Type
$ grunt
or
$ grunt mochacov
To do the auto server-side unit test.
NOTE: this section is deprecated
Once you run grunt script, the complexity report will be generated into public/test/report
folder.
The complexity report includes lines of code, cyclomatic complexity, Halstead complexity measures and the maintainability index.
According to this article, Complexity is measured by the number of paths your code can take. if-else statement for example will add 1 point. If your function is above 10, it's a good idea to refactor it.