Mô-đun khẳng định Node.js
Ví dụ
Nếu một biểu thức có giá trị là 0 hoặc sai thì sẽ xảy ra lỗi và chương trình sẽ bị chấm dứt:
var assert = require('assert');
assert(5 > 7);
Chạy ví dụ »Định nghĩa và cách sử dụng
Mô-đun khẳng định cung cấp một cách kiểm tra các biểu thức. Nếu biểu thức có giá trị là 0 hoặc sai thì sẽ gây ra lỗi xác nhận và chương trình sẽ bị chấm dứt.
Mô-đun này được xây dựng để Node.js sử dụng nội bộ.
Cú pháp
Cú pháp để đưa mô-đun khẳng định vào ứng dụng của bạn:
var assert = require('assert');
Khẳng định phương pháp
Method | Description |
---|---|
assert() | Checks if a value is true. Same as assert.ok() |
deepEqual() | Checks if two values are equal |
deepStrictEqual() | Checks if two values are equal, using the strict equal operator (===) |
doesNotThrow() | |
equal() | Checks if two values are equal, using the equal operator (==) |
fail() | Throws an Assertion Error |
ifError() | Throws a specified error if the specified error evaluates to true |
notDeepEqual() | Checks if two values are not equal |
notDeepStrictEqual() | Checks if two values are not equal, using the strict not equal operator (!==) |
notEqual() | Checks if two values are not equal, using the not equal operator (!=) |
notStrictEqual() | Checks if two values are not equal, using the strict not equal operator (!==) |
ok() | Checks if a value is true |
strictEqual() | Checks if two values are equal, using the strict equal operator (===) |
throws() |