Nodejs Learning Note--assert (assertion)

Source: Internet
Author: User

Assert-a:actual e:expected m:message o:operator v:value b:block
Assert.fail (A, E, M, O)
assert (V, m), Assert.ok (V, [m])
Assert.equal (A, E, [m])
Assert.notequal (A, E, [m])
Assert.deepequal (A, E, [m])
Assert.notdeepequal (A, E, [m])
Assert.strictequal (A, E, [m])
Assert.notstrictequal (A, E, [m])
Assert.throws (b, [ERROR], [m])
Assert.doesnotthrow (b, [m])
Assert.iferror (v)
Assert.fail (A, E, M, O)

Throws An exception This displays the values for and separated by the actual expected provided operator.

Note:always throws an exception and display the message which would be "actual operator expected" if it's omited.

e.g and assert.fail(1,1,"Always throws this message","=")assert.fail(1,1,"","+")

Assert (V, [M]), Assert.ok (V, [m])

Tests if value is truthy, it's equivalent toassert.equal(true, !!value, message);

Note:if the value isn ' t a truth-value, it throws and exception and display the message which would be "value = = true" If I T ' s omited.

e.g and assert(0)assert.ok(0,"Throws me")

Assert.equal (A, E, [m])

Tests shallow, coercive equality with the equal comparison operator ( == ).

e.g and assert.equal(1,2,"They are not equal")assert.equal(1,1,"would not show me")

Assert.notequal (A, E, [m])

Tests shallow, coercive non-equality with the not equal comparison operator ( != ).

Note: In summary, an exception is thrown when the comparison result is false.

Assert.deepequal (A, E, [m])

Tests for deep equality.

Note: not clear!

Assert.notdeepequal (A, E, [m])

Tests for any deep inequality.

Note: not clear!

Assert.strictequal (A, E, [m])

Tests strict equality, as determined by the strict equality operator ( === )

Assert.notstrictequal (A, E, [m])

Tests strict non-equality, as determined by the strict not equal operator ( !== )

Assert.throws (b, [ERROR], [m])

expects block to throw a error. error can be constructor, RegExp or validation function.

Validate instanceof Using constructor:

Assert.throws (function () {throw new Error ("wrong value");},error);

Validate error message using REGEXP:

Assert.throws (function () {throw new Error ("wrong value");},/value/);

Custom Error Validation:

Assert.throws (function () {throw new Error ("Wrong value"),},function (err) {if (err instanceof Error) &&/value/. Test (Err)) {return true;}, "Unexpected error");
Assert.doesnotthrow (b, [M])

expects not block to throw a error, see for assert.throws details.

Assert.iferror (v)

Tests if value is no a false value, throws if it is a true value. Useful when testing the first argument, in error callbacks.

Note:it throws error when the value is a truth-value!

Nodejs Learning Note--assert (assertion)

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.