admin管理员组文章数量:1429102
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();
});
本文标签: javascriptAsync testing with Karma and MochaStack Overflow
版权声明:本文标题:javascript - Async testing with Karma and Mocha - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745521266a2661636.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论