2016-12-26 2 views
0

이것은 일부 데이터를 검색하는 localeDataService입니다.

function getapplicableitems() { 
    if (profile.region === 'ASIA') { 
     return otherDataService.getOtherData() 
      .then(function(data) { 
       return data; 
      }); 
     } 
    return $q.when(false); 
} 

다음은 재스민 테스트입니다.

describe('Locale Data Service Tests:', 
function() { 

    var sut, 
     profile = {}, 
     otherDataServiceMock = {}, 
     $q; 

    beforeEach(module(function($provide) { 
     otherDataServiceMock = { 
      getOtherData: function() {} 
     }; 

     spyOn(otherDataServiceMock, 'getOtherData') 
      .and.callFake(function() { 
       var deferred = $q.defer(); 
       deferred.resolve('Remote call result'); 
       return deferred.promise; 
      }); 

     $provide.value('profile', profile); 
     $provide.value('otherDataService', otherDataServiceMock); 
    })); 

    beforeEach(inject(function(_localeDataService_, _$q_) { 
     $q = _$q_; 
     sut = _localeDataService_; 
    })); 

    it('should not call if region is NOT ASIA', function() { 
      profile.region = 'ZZZZ'; 

      spyOn(otherDataServiceMock, 'getOtherData').andReturn($q.when(false)); 

      sut.getOtherData(); 

      expect(otherDataServiceMock.getOtherData).toEqual($q.when(false)); 

    }); 

    it('should call for ASIA', function() { 
      profile.region = 'ASIA'; 

      sut.getOtherData(); 

      expect(otherDataServiceMock.getOtherData).toHaveBeenCalled(); 
     }); 
}); 

나는 일할 시험이 "지역은 아시아가 아닌 경우 호출해서는 안"얻을 수 없습니다입니다. 나는 재 스민/유닛 테스팅을 처음 사용합니다. 도와주세요.

+0

는 어디 getapplicableitems 기능 –

+0

localeDataService.getapplicableitems() 그 때는 사용되는 (기능 (데이터) {$ scope.items = 데이터; }); – Ganesh

+0

여러 위치에서 공유되는 컨트롤러와 같이 사용됩니다. – Ganesh

답변

0

이렇게 문제를 해결했습니다. region이 ZZZZ 일 때 사용되지 않으므로 otherDataServiceMock을 호출하는 포인트가 없습니다.

it('should not call if region is NOT ASIA', function() { 
     profile.region = 'ZZZZ'; 
     var noChange = true; 

     sut.getOtherData().then(function(){ 
      noChange = false; 
     }); 

     expect(noChange).toBeTruthy() 

});