javascript - Async testing with Karma and Mocha - Stack Overflow

I am using Karma + Mocha for testing an AngularJS service with an async call. How would I go about tell

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
Add a ment  | 

2 Answers 2

Reset to default 4

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();
        });
      });
    });
  });
});

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();
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745521267a4631270.html

相关推荐

  • javascript - Async testing with Karma and Mocha - Stack Overflow

    I am using Karma + Mocha for testing an AngularJS service with an async call. How would I go about tell

    11小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信