2014-06-12 1 views
0

저는 연습 문제를 완료하면서 AngularDart 자습서를 통해 작업하고 단위 테스트를 작성하려고합니다.Dart unittest가 도움이되지 않는 결과를 출력합니다.

나는 다음과 같습니다 시험이 있습니다

test('should convert sugar ingredient to maple syrup', inject((SugarFilter filter) { 
    var r1 = new Recipe(null, null, null, ['has sugar in ingredients '], 'bla', null, null); 
    var r1New = new Recipe(null, null, null, ['has maple syrup in ingredient '], 'bla', null, null); 
    var r2 = new Recipe(null, null, null,[ 'has pure ingredients'], 'bla', null, null); 
    var inList = [r1, r2]; 
    var outList = [r1New, r2]; 
    expect(filter(inList), equals(outList)); 
})); 

시험은이 출력 실패 :

Test failed: Caught Expected: [Instance of 'Recipe', Instance of 'Recipe'] 
    Actual: [Instance of 'Recipe', Instance of 'Recipe'] 
    Which: was <Instance of 'Recipe'> instead of <Instance of 'Recipe'> at location [0] 
내가 실패 있는지 확인하는 'categoryFilter'에 대한 기존 테스트를 수정 시도

나는 같은, 오히려 도움이되지 않는 산출을 얻는다.

두 개체의 비교 결과를보다 의미있는 방법으로 출력 할 수 있습니까?

답변

1

다른 개체가 포함 된 두 목록을 비교할 때 정확히 무엇을 기대합니까? 각 목록에 두 개의 Receipe 인스턴스가 포함되어 있기 때문에 두 항목이 같아야합니까? filter()은 무엇입니까?

expect(inList, equals(inList)); 

당신은 목록의 내용을 비교 everyElement, orderedEquals 또는 unorderedEquals 같은 정규 표현을 사용할 수 있습니다 : 동일한 목록 경우

두 목록은 동일하다. 그러나 같은 클래스의 다른 인스턴스라도 목록에 넣으면 comparsion은 여전히 ​​실패합니다.

comparsion의 동작 방식을 다르게하려면 Receipe 클래스의 equals() 메서드를 재정의해야합니다 (이 경우 get hashCode을 다시 정의해야 함).

더 나은 오류 메시지를 얻으려면 예를 들어 출력 문자열에 일부 필드 값을 추가하는 등의 방법으로 toString() 메서드를 재정의 할 수 있습니다.

@override 
String toString() => '${super.toString()} ${name}'; // I don't know if the Receipe class has a name field though