assert 모듈은 기능 주장에 사용되는 다양한 기능을 제공합니다. assert.notEqual은 두 객체가 같지 않아야 하는지 테스트합니다. 두 개체가 모두 같으면 어설션 오류가 발생합니다.
구문
assert.notEqual (actual, expected, [message])
매개변수
위의 매개변수는 다음과 같이 설명됩니다. -
-
실제 – 이 매개변수는 비교해야 하는 실제 값을 포함합니다.
-
예상 – 이 매개변수는 실제 매개변수에 대해 평가될 예상 값을 보유합니다.
-
메시지 – 이것은 선택적 매개변수입니다. 함수 실행 시 출력되는 사용자 정의 메시지입니다.
Assert 모듈 설치
npm install assert
assert 모듈은 내장된 Node.js 모듈이므로 이 단계도 건너뛸 수 있습니다. 최신 assert 모듈을 얻으려면 다음 명령을 사용하여 assert 버전을 확인할 수 있습니다.
npm version assert
함수에서 모듈 가져오기
const assert = require("assert").strict; 예시
notDeepStrictEqual.js라는 이름의 파일을 만들고 아래 코드 조각을 복사합니다. 파일을 생성한 후 아래 명령을 사용하여 이 코드를 실행하십시오.
node notEqual.js
notDeepStrictEqual.js
// Importing the module
const assert = require('assert').strict;
var a = 3;
var b = "3";
try {
// Checking if a & b are equal
assert.notEqual(a, b)
console.log("a & b are not equal")
} catch(error) {
console.log("Error Occured: ", error)
} 출력
C:\home\node>> node notEqual.js a & b are not equal
예시
예를 하나 더 살펴보겠습니다.
// Import the module
const assert = require('assert').strict;
var a = 3;
var b = 3;
try {
// Checking if a & b are equal
assert.notEqual(a, b)
console.log("a & b are not equal")
} catch(error) {
console.log("Error Occured: ", error)
} 출력
C:\home\node>> node notEqual.js
Error Occured: { AssertionError [ERR_ASSERTION]: Identical input passed to
notStrictEqual: 3
at Object.<anonymous> (/home/node/test/assert.js:10: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: 3,
expected: 3,
operator: 'notStrictEqual' }