Javascript unit test: jasminejs 2.0 troubles

Source: Internet
Author: User

I am a supporter of TDD. I used to write back-end generations. I like to extract some important logic code and add Unit Test. However, when I recently came into contact with the front-end Unit Test, I couldn't help but think about it. By the way, we can achieve this year's goal (at least one technical blog in a month, and I will make up for it in May ). There are a lot of front-end testing frameworks, and Qunit and Jasminejs are the traffic comparison. The specific differences between the two multi-unit testing frameworks are as follows: there are a lot of information on the Internet. If you are interested, please check. I am attracted by Jasminejs's rich assertions, asserted extensions, spy, and mock features. So first introduce jasminejs into the project. Version 1.3 was introduced at that time. I recently learned about version 2.0 and found that the jasminejs code of Version 2.0 is introduced, so my entire Unit Test cannot run. Why not? The incompatibility problem made me feel that jasminejs is a little pediatric. Thanks to some encapsulation, I cannot upgrade it. The introduction to jasminejs on the internet is mostly for the version 1.3. The following is my troubleshooting and Summary experience. I will take a note for myself and share it with you. Please make a picture of the error. This article only describes the differences between 1.3 and 2.0. As for the jasminejs detailed learning manual, we suggest you go to the official website and introduce it in detail. Jasminejs: http://jasmine.github.io/jasminejs github: https://github.com/pivotal/jasmine/ differences: 1, calldifferences: Test code: Copy Code describe ("test the spy function", function () {var foo, bar = null; beforeEach (function () {foo = {setBar: function (value) {bar = value ;}; spyOn (foo, 'setbar '); // foo is the spy function foo. setBar (123); foo. setBar (456, 'Another param');}); it ("last called parameter", function () {var most = foo. setBar. CILS; reverse CT (Most). toBeDefined () ;}); copy the code version 1.3:, chrome tracking result. CILS is an array that returns the context of each call. The last value of the array is equal to the value of mostRecentCall. Except CT (foo. setBar. CILS [foo. setBar. call. length-1]). toEqual (foo. setBar. mostRecentCall); version 2.0:, chrome tracking result. CILS is an instance object of CallTracker. In this way, the call-related information is encapsulated with classes. All the methods in the class are described below. 2. 1.3 mostRecentCall and 2.0.calls.mostRecent () differences (1) Different call methods the former is called method. mostRecentCall, for example, foo. setBar. mostRecentCall: The method to be called. CILS. mostRecent, for example: foo. setBar. CILS. mostRecent () (2) is an object with the same value. mostRecentCall has two attributes: args and object. args indicates that the call method is a passed parameter, and the object indicates the object to which the call method belongs. Copy the Code describe ("test the spy function", function () {var foo, bar = null; beforeEach (function () {foo = {setBar: function (value) {bar = value ;}}; spyOn (foo, 'setbar'); // foo is the spy function foo. setBar (123); foo. setBar (456, 'Another param');}); it ("parameters called last time", function () {CT (foo. setBar. mostRecentCall. args [0]). toEqual (456) ;}); it ("parameter called last time", function () {CT (foo. setBar. mostRecentCall. object = foo ). toEqual (True) ;}); copy code. CILS. mostRecent () is a function that returns an object, same as above. 3. The powerful callobject. call. any () in 2.0 is called at least once, and false/true is returned.. CILS. count () number of calls. CILS. argsFor (index) returns the parameter called more than once by index. The returned value is []. CILS. argsFor (index ). CILS. allArgs () returns all called parameters separated by commas. Each called parameter is returned in the form of an array. CILS. all () is the same as in 1.3. Returns the context of each call.. CILS. mostRecent () is the same as the mostRecentCall of 1.3, and the context information of the last call is returned. Equivalent. CILS. all ()[. CILS. call (). length-1]. CILS. first is equivalent. CILS. all () [0]. CILS. reset () resets all spy information. call. any () returns false. 4. asynchronous Ah asynchronous 1.3 In order to Implement Asynchronous testing, jasminejs uses a very basic method to wait cyclically. Copy the Code describe ("Asynchronous specs", function () {var value, flag; it ("shocould support async execution of test preparation and expectations", function () {runs (function () {flag = true; value = 0; setTimeout (function () {flag = true ;}, 500) ;}); waitsFor (function () {value ++; return flag ;}, "The Value shocould be incremented", 450); runs (function () {CT (value ). toBeGreaterThan (0) ;}); copy the code The asynchronous method has the following problems: 1. Writing is too complicated and needs to be encapsulated by itself; 2. Implementation is a bit elementary. WaitsFor has three parameters: 1st parameters: Check whether the asynchronous function is complete, return bool, return true, stop loop wait; 2nd parameters: Error prompt, if the number of milliseconds specified by the Operation's 3rd parameters is still false for the 1st parameters, the assertion fails. The message is as follows: 3rd parameters: cycle wait time, in milliseconds. Asynchronous implementation is mostly ajax requests. This request is affected by many factors such as the network and server environment, so it is difficult to estimate how long it takes. 2.0 this async test was greatly optimized and more reasonable. Copy the Code describe ("Asynchronous specs", function () {var value; beforeEach (function (done) {setTimeout (function () {value = 0; done ();}, 1) ;}); it ("shocould support async execution of test preparation and expectations", function (done) {value ++; CT (value ). toBeGreaterThan (0); done () ;}); the Code to be copied adopts a pub/sub subscription mode for processing. The ajax request is successful and it is notified to be executed. 5. The main functions are the same as those below, but the writing style changes. 1.3 and 2.0 table 1.3 2.0 description andCallThrough. and. callThrough function monitor, but the function actually executes andReturn. and. returnValue function monitor. The function is not actually executed. Specifies the returned value of the monitored function andCallFake. and. callFake replaces the monitored function. The original function does not execute CILS. CILS. all () returns the context contextmostRecentCall of each call. CILS. the context information jasmine of the last call of mostRecent. clock. useMock jasmine. clock (). install start simulation time jasmine. clock. tick jasmine. clock (). tick simulates the forward millisecond jasmine. clock (). uninstall () stops the simulation time. If this method is not available in 1.3, write these statements first. If other differences are necessary, I will add them in subsequent articles. I will add some source code analysis articles for jasmine2.0 without any need.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.