A recent study of the Jasmine framework has yielded considerable benefits. Based on a company colleague's documentation, here are some of the features of the Jasmine framework.
One: Introduction
The Jasmine Framework is a behavioral-driven development framework for testing JavaScript code that does not rely on any other JavaScript framework, does not require a DOM, and has a very simple and clear syntax.
Second: Scope of application
Because the Jasmine framework does not rely on browsers, DOM, or any JavaScript framework, it is especially suitable for unit test sites, node.js projects, or any project that has JavaScript running.
Small Example 1: For example, to test if the real value is smaller than expected
Beforeeach (function () {
this.addmatchers ({
tobelessthan:function (expected) {
var actual = this.actual;
var nottext = This.isnot? "Not": "";
This.message = function () {return
"expected" + actual + Nottext + "to be less than" + expected;
}
return actual < expected;}});
Small Example 2: For example, to test Ajax or other asynchronous behavior:
var klass = function () {
};
Klass.asyncmethod = function (callback) {
Someasynccall (callback);
};
...
It (' should test async call ', function () {
Spyon (Klass, ' Asyncmethod ');
var callback = Jasmine.createspy ();
Klass.asyncmethod (callback);
Expect (callback). not.tohavebeencalled ();
var someresponsedata = ' foo ';
Klass.asyncmethod.mostrecentcall.args[0] (someresponsedata);
Expect (callback). Tohavebeencalledwith (Someresponsedata);
});
Three: Why do we choose the Jasmine Framework?
Although in the market there are many kinds of JS Unit test framework, such as Testswarm, Jstestdriver,buster.js,yui Yeti,sinon, but many unit test framework only support and browser work together, Some unit test frameworks cannot support testing of asynchronous callback code. Some of the code is difficult to understand for the IDE, however, the Jasmine framework is a good framework for the advantages of various frameworks.
Four: The excellent design principle of jasmine framework:
(1) A good JS unit test framework should not be integrated into any browser, framework, platform or host machine language settings.
(2) A good JS unit test framework should be in line with the JS language habits of the grammar.
(3) Good JS Unit test framework should be in any JS can run the place
(4) A Good JS unit test framework should be non-intrusive to the application being tested.
(5) A Good JS unit test framework should be able to integrate well with the IDE, such as through the IDE's static code quality checks.
(6) A good JS unit test framework should be simple enough and easy to get started. I've learned it in 5 minutes, and then I wrote the first demo and ran it.