I want to test if my service Call was successfull if the Function "search" was called.
My Controller:
function UnitTestsCtrl(homePSrv, $scope) {
var that = this;
this.SearchModel = {};
this.homePSrv = homePSrv;
}
UnitTestsCtrl.prototype.Init = function() {
var that = this;
this.homePSrv.InitUnitTestSearchModel().then(function(result) {
that.SearchModel = result;
that.Search();
});
}
angular.module("unitTestsCtrl", [])
.controller("unitTestsCtrl", UnitTestsCtrl);
The jasmine unit test:
describe('Controller Tests: "UnitTestsCtrl"', function () {
var ctrl, mockHomePSrv;
beforeEach(function () {
mockHomePSrv = jasmine.createSpyObj('homePSrv', ['InitUnitTestSearchModel']);
module('app.main');
inject(function ($controller, $q) {
mockHomePSrv.InitUnitTestSearchModel.and.returnValue($q.when('InitUnitTestSearchModelData'));
ctrl = $controller('unitTestsCtrl', {
homePSrv: mockHomePSrv,
});
});
});
it('Funktion "Search" was called', function () {
spyOn(ctrl, 'Init');
spyOn(ctrl, 'Search');
ctrl.Init();
expect(ctrl.Init).toHaveBeenCalled();
//Thats not working - its not being called
expect(ctrl.Search).toHaveBeenCalled();
});
});
The problem is that the expectation if the search function was called is false. I think it has to do with the promise.
Aucun commentaire:
Enregistrer un commentaire