Node-related--testing

Source: Internet
Author: User

Test:

  • Assert module; Node comes with
    var assert = require (' assert '); var now = Date.now (), Console.log (now), Assert.ok (now% 2 = = 0),----------------------------------------var request = Require (' superagent '); var assert = require (' assert '); Request.get (' http://localhost:3000 ')       . Send ({q: ' bieber '})       . End (function (res) {        //Assert to determine if the response status code is correct        Assert.ok (res.status);        Asserts if the keyword exists        assert.ok (~res.text.tolowercase (). indexOf (' Bieber '));        Asserts if the list item exists        Assert.ok (~res.text.indexof (' <li> '));       }) ;
  • Ecpect.js, to optimize the way the Assert code is written; API:
    • OK: The assertion is true:
      Expect (1). To.be.ok (); expect (true). To.be.ok (); expect ({}). To.be.ok (); expect (0). To.not.be,ok ();
    • Be/equal: = = =

      Expect (1). to.be (1); expect (Nan). not.to.equal (Nan); expect (1). not.to.be (True);
    • EQL: Assertion is not strictly equal, support object

      Expect ({A: ' B '}). TO.EQL ({A: ' B '}), expect (1). TO.EQL (' 1 ');
    • A/an: Asserts the Owning property type, supports the instanceof of the array
      Expect (5). To.be.a (' number '), expect ([]). to.be.an (' array '), expect ([]). to.be.an (' object ');//constructorexpect (5). To.be.a (' number '); expect ([]). to.be.an (' Array '); expect (Tobi). To.be.a (Ferrect); instanceof
    • Match: Asserts whether a string matches a regular expression
      Expect (program.version). To.match (/[0-9]+\.[ 0-9]+\. [0-9]+/];
    • Contain: Asserts whether the string contains another string;
      Expect ([up]). To.contain (1); expect (' Hello World '). To.contain (' World ');
    • Length: assert array lengths;
      Expect ([]). To.have.length (0); expect ([i]). To.have.length (3);
    • Empty: Asserts whether the array is empty;
      Expect ([]). To.be.empty (); expect ([]). To.not.be.empty ();
    • Property: Asserts whether an attribute/value exists for itself;
      Expect (window). To.have.property (' expect '); expect (window). To.have.property (' expect ', expect); expect ({a: ' B '}). To.have.property (' a ');
    • Key/keys: Assertion key exists, support only modifier;
      Expect ({A: ' B '}). To.have.key (' a '); expect ({a: ' B ', C: ' d '}). To.only.have.keys (' A ', ' C '); expect ({a: ' B ', C: ' d '}). To.only.have.keys ([' a '. ') C ']); expect ({a: ' B ', C: ' d '}). To.not.only.have.keys (' a ');
    • ThrowException: Asserts whether the function throws an exception when it is called;
      Expect (FN). To.throwexception (); expect (FN2). To.not.throwException ();
    • Within: Asserts whether the array is within a certain interval;
      Expect (1). To.be.within (0,infinity);
    • Greaterthan/above: >
      Expect (3). To,be.above (0); expect (5). To.be.greaterThan (3);
    • Lessthan/below: <
      Expect (0). To.be.below (3); expect (1). To.be.lessThan (3);

Moncha: test Framework

  • Example:
    • Test.js
      Describe (' A topic ', function () {    It (' Should test something ', function () {     });    Describe (' anthor topic ', function () {        It (' should test something else ', function () {})})    });
    • Run: Mocha test.js; Report list form: Mocha-r list test.js
  • Test Async Code: Mocha The default is to execute the other immediately after one test case executes, but sometimes you want to delay the execution of the next test case;
    It (' should not know ', function (done) {   setTimeout (function () {      Assert.ok (1 = = 1);      Done ();   }, ();   });

    If there are many asynchronous operations in a test case, you can add a counter:

    It (' should complete three requests ', function (done) {  var = 3;  Request.get (' HTTP://LOCALHOST:3000/1 ', function (res) {     if (+ = res.status) throw new error (' Request Error ');-- Total | | Done ();  });  Request.get (' HTTP://LOCALHOST:3000/2 ', function (res) {     if (+ = res.status) throw new error (' Request Error ');-- Total | | Done ();  });  Request.get (' HTTP://LOCALHOST:3000/3 ', function (res) {     if (+ = res.status) throw new error (' Request Error ');-- Total | | Done ();  });})
  • BDD style: The previous Test example style is BDD (behavior-driven development);
  • TDD style: test-driven development, organized using test sets (suit) and tests (test), each with setup and TEARDOWM functions, which are executed before the tests in the test set are executed, in order to avoid duplication of code that has maximized the independence of the tests;
    Suite (' Net ', function () {   Suite (' Stream '), function () {      var  client;      Suitesetup (function () {         client = Net.connect (+, ' localhost ');      });       Test (' Connect event ', function (done) {         Client.on (' Connect ', done);      });       Test (' receiving data ', function (done) {        client.write (');        Client.once (' data ', done);      });       Suiteteardown (function () {        client.end ();})})   })
  • Export style: Use the node module system to output the test; Each export key represents a test set, and nested test sets can be represented by child objects
    Exports. Array = {   ' #indecOf () ': {      ' should return-1 when the value was not present ': function () {},      ' should retur n the correct index when the value is present ': function () {}}   }

Use mocha on the browser side:

Node-related--testing

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.