Kratos
Kratos

Reputation: 1114

Unit-Testing with Karma not calling functions

I am trying to perform unit testing with Karma. I have done everything according to the documentation. When I write this part of the test that follows it never calls the last two functions.

it('should create the mock object', function (done) {

    service.createObj(mockObj)
        .then(test)
        .catch(failTest)
        .finally(done);
});
var test = function() {
    expect(2).toEqual(1);
};

var failTest = function(error) {
    expect(2).toEqual(1);
};

Upvotes: 0

Views: 909

Answers (2)

Moncef Hassein-bey
Moncef Hassein-bey

Reputation: 1361

  • Install angular-mocks module.
  • Inject module with module in beforeEach.
  • Inject your service with inject function in beforeEach.
  • Use $httpBackend to simulate your server.
  • Do, not forget, to make it, sync. with $http.flush().

Upvotes: 0

Dawid Pawłowski
Dawid Pawłowski

Reputation: 162

Try to inject into your beforeEach function rootScope. For example like this:

var rootScope;

beforeEach(inject(function (_$rootScope_) {
    rootScope = _$rootScope_.$new();
    //other injections
}));

and next invoke $digest() after your service method:

it('should create the mock object', function (done) {
    service.createObj(mockObj)
        .then(test)
        .catch(failTest)
        .finally(done);

    rootScope.$digest();
});

Upvotes: 1

Related Questions