2013-11-03 6 views
3

나는 비동기 호출로 AngularJS 서비스를 테스트하기 위해 카르마 + 모카를 사용하고 있습니다. 테스트에서 비동기 호출을 수행했음을 알리려면 어떻게해야합니까? 즉, 표준 Mocha done() 함수는 어디로 이동합니까?카르마와 모카로 비동기 테스트

var should = chai.should(); 
describe('Services', function() { 
    beforeEach(angular.mock.module('myApp')); 
    describe('sampleService', function(){ 
    it.only('should return some info', angular.mock.inject(function(sampleService) { 
     sampleService.get(function(data) { 
     data.should.equal('foo'); 
     //done() 
     }); 
    })); 
    }); 
}); 

답변

3

Duh ... 나는 그것을 알고 있었다.

var should = chai.should(); 
describe('Services', function() { 
    beforeEach(angular.mock.module('myApp')); 
    describe('sampleService', function(){ 
    it.only('should return some info', function(done) { 
     angular.mock.inject(function(sampleService) { 
     sampleService.get(function(data) { 
      data.should.equal('foo'); 
      done(); 
     }); 
     }); 
    }); 
    }); 
}); 
1

다음은 유용한 패턴입니다. 주입에 앞서 테스트를 수행하고 약속과 함께 작동합니다. 제 경우에는 이것을 사용하여 (LoginService 호출에 의한) http 응답에 대한 인터셉터 처리를 검증합니다.

var LoginService, mockBackend; 

beforeEach(function() { 
    module('main.services'); 

    inject(function(_LoginService_, $httpBackend) { 
    LoginService = _LoginService_; 
    mockBackend = $httpBackend; 
    }); 
}); 

describe('login', function() { 
    it(' auth tests', function(done) { 

    var url = '/login'; 

    mockBackend.expectPOST(url) 
    .respond({token: 'a.b.c'}); 

    LoginService.login('username', 'pw') 
    .then(function(res) { 
     console.log(' * did login'); 
    }) 
    .finally(done); 

    mockBackend.flush(); 
    }); 

}); 

afterEach(function() { 
    mockBackend.verifyNoOutstandingExpectation(); 
    mockBackend.verifyNoOutstandingRequest(); 
});