Nodejs Assertion Test

Source: Internet
Author: User

var assert = require (' assert ');

/*node, we can use the Assert module to test the code.
Equal () and notequal () are judged for equality and inequality respectively,
The first parameter is expected, the second argument is the real value, and the third parameter is the exception information */

Assert.equal (1,2,[console.log (' OK ')]);//The expected and true values, whether equal or unequal, output information, why?
Assert.equal (1,2,[' not Equal '));//The expected and true values are not equal, throwing exception information

The/*ok () method is a concise way to compare truth values, equivalent to comparing the current value to true with = =. */

Assert.ok (', ' w ');//Space is False
Assert.ok (' This was a string ', ' Strings that was not empty and is truthy '); True
Assert.ok (0, ' Zero is not truthy ');//0 to False

Assert.fale (' Not Equal ', Console.log (' Error '));

/*node provides a comparison method for object objects deepequal () and notdeepequal (),
They use the following steps to compare objects, throwing an exception with a step mismatch:
1. Use = = = comparison;
2. Compare whether they are buffers, if it is the comparison length, followed by the comparison of each byte per byte;
3. Compare by = =; 4. Finally, if the arguments are object objects, they are compared to the property length and property values.
As you can see, these two methods may be less performance, so use them only when you need them. */

Assert.deepequal (1,2,[' not Equal ');

/*assert.throws (block[, error][, message])
Declares a block used to throw an error (error), and error can be a constructor, regular expression, or other validator. */

Assert.throws (function () {
throw new Error (' message is wrong ');
// });
Assert.doesnotthrow (function () {
throw new Error ("I lived in the ocean" before Nemo);
// });

Nodejs Assertion Test

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.