0

내가 업데이트 내 테스트하고 돌아 오는 오류 메시지가 매우 혼란 때문에했습니다 ...재스민 테스트 녹아웃 ObservableArray

it('Should get the available databases', function() { 
     expect(vm.databases).toEqual([ 
      { 
       name: 'DB 1', 
       chosenRoles: function() { return []; }, 
       chosenModules: function() { return []; } 
      }, 
      { 
       name: 'DB 2', 
       chosenRoles: function() { return []; }, 
       chosenModules: function() { return []; } 
      }, 
      { 
       name: 'DB 3', 
       chosenRoles: function() { return []; }, 
       chosenModules: function() { return []; } 
      }, 
      { 
       name: 'DB 4', 
       chosenRoles: function() { return []; }, 
       chosenModules: function() { return []; } 
      }, 
      { 
       name: 'DB 5', 
       chosenRoles: function() { return []; }, 
       chosenModules: function() { return []; } 
      } 
     ]); 
    }); 

Summary (1 tests failed) 
x User Management Initial state Should get the available databases 
    Expected [ { name : 'DB 1', chosenRoles : Function, chosenModules : Functi 
}, { name : 'DB 2', chosenRoles : Function, chosenModules : Function }, { nam 
: 'DB 3', chosenRoles : Function, chosenModules : Function }, { name : 'DB 4', 
hosenRoles : Function, chosenModules : Function }, { name : 'DB 5', chosenRole 
: Function, chosenModules : Function } ] to equal [ { name : 'DB 1', chosenRol 
: Function, chosenModules : Function }, { name : 'DB 2', chosenRoles : Functi 
, chosenModules : Function }, { name : 'DB 3', chosenRoles : Function, chosenM 
ules : Function }, { name : 'DB 4', chosenRoles : Function, chosenModules : Fu 
tion }, { name : 'DB 5', chosenRoles : Function, chosenModules : Function } ]. 

원래의 게시물

나는를

업데이트 Jasmine Tests를 실행하는 데 새로운 것이므로 쉬운 질문 일 수 있지만 상황에 맞는 것을 찾을 수는 없습니다.

각 오브젝트에서 두 개의 녹아웃 observableArray()가있는 배열에서 밑줄을 사용하여 다섯 개의 오브젝트 목록을 만듭니다.

var pfp = pfp || {}; 
pfp.insight = pfp.insight || {}; 
pfp.insight.controllers = pfp.insight.controllers || {}; 

(function() { 
    'use strict'; 

    this.settingsController = function() { 
     var root = this; 
     root.databases = _.range(5).map(function (i) { 
      return { 
       name: 'DB ' + (i + 1), 
       chosenRoles: ko.observableArray(), 
       chosenModules: ko.observableArray() 
      }; 
     }); 
    }; 
}).call(pfp.insight.controllers); 

나는 데이터베이스 배열의 초기 상태를 확인하는 단위 테스트를 작성하는 방법을 정확히 모르겠습니다. 나는이 문제를 추측 할

describe('User Management', function() { 
'use strict'; 

var vm, 
    databases = []; 

describe('Initial state', function() { 
    beforeEach(function() { 
     vm = new pfp.insight.controllers.settingsController(); 
    }); 

    it('Should get the available databases', function() { 
     expect(vm.databases).toEqual([ 
      { 
       name: 'DB 1', 
       chosenRoles: [], 
       chosenModules: [] 
      }, 
      { 
       name: 'DB 2', 
       chosenRoles: [], 
       chosenModules: [] 
      }, 
      { 
       name: 'DB 3', 
       chosenRoles: [], 
       chosenModules: [] 
      }, 
      { 
       name: 'DB 4', 
       chosenRoles: [], 
       chosenModules: [] 
      }, 
      { 
       name: 'DB 5', 
       chosenRoles: [], 
       chosenModules: [] 
      } 
     ]); 
    }); 
}); 

답변

2

ko.observableArray()의 대 []의 평등을 비교에있다. 아마 당신의 라인을 따라 더 테스트 주장을 나눌 수있다 : 당신의 주장에

expect(vm.databases.length).toEqual(5); 
expect(vm.databases[0].name).toEqual('DB 1'); 
expect(vm.databases[0].chosenRoles.length).toEqual(0); 
expect(vm.databases[0].chosenModules.length).toEqual(0); 
... 
expect(vm.databases[4].name).toEqual('DB 5'); 
expect(vm.databases[4].chosenRoles.length).toEqual(0); 
expect(vm.databases[4].chosenModules.length).toEqual(0); 
1

객체는 자바 스크립트 배열로 chosenRoleschosenModules 있습니다. 그러나 당신은 그것을 관찰 가능한 배열로 만들고 있습니다. 그것은 그들이 실제로 기능을한다는 것을 의미합니다.

내가 마이크 그리피스 위의 제안 일을하지만, 주장에 대한 함수 호출을 (괄호를 추가)하기 위해 기억 (당신이 엄격한 평등을 얻을 수 있도록 toEqualtoBe에 변경) 할 수도 있습니다.

expect(vm.databases[0].chosenRoles().length).toBe(0); expect(vm.databases[0].chosenModules().length).toBe(0);