2014-04-07 7 views
2

야 그래서 오늘은 재스민과 장난하고 있었고, 난이 간단한 Person 객체를 작성비교 객체

function Person() {} 
Person.prototype.name = "Alex"; 
Person.prototype.age = 24; 

여기 내 사양 테스트

describe("Person", function() { 
var someone = new Person(); 
it('should be equal to other people', function() { 
var another = { 
name: "Joe", 
age: 25, 
}; 
expect(someone).toEqual(another); 
}); 
}); 

이야 그러나 그것은 실패에} {예상 equal {name : 'Alex', age : 24} Jasmine의 toEqual 매처로 개체를 조작하면 안됩니까? 내가 여기서 뭔가를 놓치고 있니?

감사합니다.

답변

3

소스 코드를 살펴보면 재스민의 toEqual 일치 항목은 궁극적으로 객체를 비교할 때 hasOwnProperty을 사용함을 알 수 있습니다. 이 코드 스 니펫 matchersUtil.js에서 확인할 수 있습니다. 함수는 동일한 파일에 in this wayeq function에 사용되는

function has(obj, key) { 
    return obj.hasOwnProperty(key); 
} 

:

// Deep compare objects. 
    for (var key in a) { 
    if (has(a, key)) { 
     // Count the expected number of properties. 
     size++; 
     // Deep compare each member. 
     if (!(result = has(b, key) && eq(a[key], b[key], aStack, bStack, customTesters))) { break; } 
    } 
    } 

... annnndutil.equals가 호출 될 때 eq 함수 toEqual.js 정합에 의해 사용된다.

toEqual 정규 표현식은 hasOwnProperty을 사용하기 때문에 비교할 때 프로토 타입 체인의 속성을 '확인'하지 않고 직접 객체의 속성 만 표시합니다.

한 주 : 프로토 타입 속성이있는 경우 hasOwnProperty의 재스민의 사용으로 라인 업하고 그건

Expected { } to equal {name: 'Joe', age: 25 }

someone가 비어 나타납니다 코드를 사용하여, 나는 시험에 대한 약간이지만 상당히 다른 결과를 얻었다 무시.