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()
});
}));
});
});
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()
});
}));
});
});
Share
Improve this question
asked Nov 3, 2013 at 3:52
cyberwombatcyberwombat
40.3k42 gold badges184 silver badges267 bronze badges
2 Answers
Reset to default 4Duh... 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();
});
});
});
});
});
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();
});