[英]Async testing with Karma and Mocha
我使用Karma + Mocha來測試帶有異步調用的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()
});
}));
});
});
呃......我知道。
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();
});
});
});
});
});
這是我發現有用的模式; 在測試之前完成注入,並且使用promises ..在我的情況下,我使用它來驗證我的攔截器對http響應的處理(來自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();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.