Pregunta

I have a factory, which I am attempting to unit test, with an injected PouchDB wrapper. The issue I am running into is that I have the mock PouchDB service returning a promise and while the promise resolves (I can console.log out the successCb and it looks correct), the karma expect statements within the successCb function where the correct console.log output is, are not running.

I am not sure why the console.log output works fine but the expect statements do not execute, any help on this matter would be greatly appreciated!

The following are the relevant snippets of code:

[ Unit Testing Setup ]

var mockCalendarsResource, mockPouchDB;
var mockPouchDB = function(name) {
    this.name = name;
    this.post = function(newCalendar, successCb, errorCb) {
        var promise = new Promise(function(successCb, errorCb) {
            newCalendar._id = 2;
            successCb(newCalendar);
        });
        return promise;
    };
}

beforeEach(function() {
    module(function($provide) {
        $provide.value('Pouch', mockPouchDB);
    });
    angular.mock.inject(function($injector) {
        mockCalendarsResource = $injector.get('Calendar');
    });
});

[ Karma Unit Test Code ]

describe('The calendar resource function create', function() {
    it('should create a new calendar', inject(function(Calendar) {
        var result = mockCalendarsResource.create({
            name: "Testing"
        },
        function(response) {
            //console.log here works correctly //
            //These are the expect statements not functioning //
            expect(response._id).toBe(2);
            expect(response.name).toBe('Testing');
            // Could put any expectation here and it will pass or not be checked //
        }, function(err) {

        });
    }));
});
¿Fue útil?

Solución

I believe you should be using Jasmine 2.0's done() to signal that the test has finished. If you don't then the async tests will not finish.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top