배열은 두 가지 방법으로 비교할 수 있습니다 -
-
메모리에 있는 동일한 배열 개체를 참조합니다.
-
다른 개체를 참조할 수 있지만 내용은 모두 동일합니다.
예시
사례 1의 경우 jasmine은 toBe 메소드를 제공합니다. 이것은 참조를 확인합니다. 예를 들어,
describe("Array Equality", () => {
it("should check for array reference equility", () => {
let arr = [1, 2, 3];
let arr2 = arr
// Runs successfully
expect(arr).toBe(arr2);
// Fails as references are not equal
expect(arr).toBe([1, 2, 3]);
});
}); 출력
이것은 출력을 줄 것입니다 -
1) Array Equality should check for array equility Message: Expected [ 1, 2, 3 ] to be [ 1, 2, 3 ]. Tip: To check for deep equality, use .toEqual() instead of .toBe().
예시
사례 2의 경우 toEqual 메서드를 사용하고 배열을 심층적으로 비교할 수 있습니다. 예를 들어,
describe("Array Equality", () => {
it("should check for array reference equility", () => {
let arr = [1, 2, 3];
let arr2 = arr;
// Runs successfully
expect(arr).toEqual(arr2);
// Runs successfully
expect(arr).toEqual([1, 2, 3]);
});
}); 출력
이것은 출력을 줄 것입니다 -
1 spec, 0 failures