Reputation: 40104
I am using Karma + Mocha for testing an AngularJS service with an async call. How would I go about telling the test that I am done with the async call - i.e. where does the standard Mocha done() function go?
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()
});
}));
});
});
Upvotes: 4
Views: 3885
Reputation: 1138
Here's a pattern I've found useful; injection done ahead of the test, and works with promises.. In my case, I use this to validate my interceptor's handling of the http response (from the call made by LoginService).
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();
});
Upvotes: 1
Reputation: 40104
Duh... I knew that.
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();
});
});
});
});
});
Upvotes: 4