Computer >> Máy Tính >  >> Lập trình >> Javascript

Hàm khẳng định.notDeepEqual () trong Node.js

Mô-đun khẳng định cung cấp một loạt các chức năng khác nhau được sử dụng để xác nhận chức năng. Assert.notDeepEqual () kiểm tra sự bất bình đẳng giữa các tham số thực tế và dự kiến. Ngoài ra, các thông số không được sâu bằng nhau. Sẽ xảy ra lỗi nếu điều kiện không được đáp ứng.

Cú pháp

assert.notDeepEqual(actual, expected[, message])

Tham số

Các thông số trên được mô tả như bên dưới -

  • thực tế - Tham số này sẽ giữ giá trị thực tế cần được so sánh.

  • dự kiến ​​ - Điều này sẽ giữ giá trị tham số dự kiến ​​cần được kiểm tra.

  • tin nhắn - Đây là một tham số tùy chọn. Đây là thông báo do người dùng xác định được in khi chức năng được thực thi và bất kỳ lỗi nào xảy ra.

Cài đặt Mô-đun Assert

npm install assert

Mô-đun khẳng định là một mô-đun Node.js có sẵn, vì vậy bạn cũng có thể bỏ qua bước này. Bạn có thể kiểm tra phiên bản xác nhận bằng cách sử dụng lệnh sau để tải mô-đun xác nhận mới nhất.

npm version assert

Nhập mô-đun trong chức năng của bạn

const assert = require("assert").strict;

Ví dụ

Tạo một tệp với tên - khẳng địnhNotDeepEqual.js và sao chép đoạn mã bên dưới. Sau khi tạo tệp, hãy sử dụng lệnh dưới đây để chạy mã này.

node assertNotDeepEqual.js

khẳng địnhNotDeepEqual.js

// Importing the module
const assert = require('assert').strict;
try {
   // Both the values should not be identical
   assert.notDeepEqual({ a: '21' }, { a: '21' });
} catch(error) {
   console.log("Error: ", error)
}

Đầu ra

C:\home\node>> node assertNotDeepEqual.js
Error: { AssertionError [ERR_ASSERTION]: Identical input passed to
notDeepStrictEqual:
{
   a: '21'
}
      at Object.<anonymous> (/home/mayankaggarwal/mysql-test/assert.js:6:9)
      at Module._compile (internal/modules/cjs/loader.js:778:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
      at Module.load (internal/modules/cjs/loader.js:653:32)
      at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
      at Function.Module._load (internal/modules/cjs/loader.js:585:3)
      at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
      at startup (internal/bootstrap/node.js:283:19)
      at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
   generatedMessage: true,
   name: 'AssertionError [ERR_ASSERTION]',
   code: 'ERR_ASSERTION',
   actual: { a: '21' },
   expected: { a: '21' },
   operator: 'notDeepStrictEqual' }

Ví dụ

Hãy xem thêm một ví dụ.

// Importing the module
const assert = require('assert').strict;

try {
   // Both the values should not be identical
   assert.notDeepEqual({ a: 21 }, { a: '21' });
   console.log("Values are not identical")
} catch(error) {
   console.log("Error: ", error)
}

Đầu ra

C:\home\node>> node assertNotDeepEqual.js
Values are not identical

Chúng ta có thể thấy trong ví dụ trên rằng một giá trị thuộc kiểu chuỗi trong khi giá trị còn lại thuộc kiểu số nguyên, đó là lý do tại sao chúng không bằng nhau.