Reputation: 345
I'm trying to test functionality which is basically doing,
My controller function is;
openPopup() {
confirmationModal.open().then((result) => {
if(result === 'OK') {
someService.doSomething()
.then(() => {
showSuccess();
});
}
}).finally(() => {
confirmationModal.close();
});
}
And my test is;
describe('confirmation modal', () => {
beforeEach(() => {
inject(($controller, _$q_) => {
var q = _$q_;
someService = {
doSomething: jasmine.createSpy()
};
var modalResult = {
then: function(callback) {
callback("OK");
}
};
confirmationModal = {
open: jasmine.createSpy().and.returnValue(q.when({ result: modalResult })),
close: jasmine.createSpy()
};
Ctrl = $controller('MainController', {
$scope: scope, confirmationModal: confirmationModal, someService: someService
});
});
});
it('should pass OK value', () => {
Ctrl.openPopup();
scope.$digest();
expect(someService.doSomething).toHaveBeenCalled();
});
});
When I run this through Karma - PhantomJS, I'm getting Expected spy unknown to have been called. which is I assume I can't go through If condition with modal result. Basically need to test conditional modal result If I can. When I test and expect open or close functions of confirmationModal, test passes but If I expect function after confirmationModal has been called, test fails.
I'm struggling with this maybe basic thing and I'm really sorry already If I miss some rules or my bad english.
Thanks already!
Upvotes: 2
Views: 1713
Reputation: 223104
In order for stubbed service to be used by the app it should be injected, and currently it is not injected. In controller it can be injected with $controller
local dependencies:
Ctrl = $controller('MainController', {
$scope: scope, confirmationModal, someService
});
result
is supposed to be a string, not an object and especially not an object that simulates promises like modalResult
does. It is
confirmationModal = {
open: jasmine.createSpy().and.returnValue(q.when('OK')),
close: jasmine.createSpy()
};
Upvotes: 1