I have the following directive:
oAccountUserModule.directive('accountUserManagement', ['AccountUserData', function(AccountUserData) {
return {
scope: false,
template: strAccountUserManagementHTML,
controllerAs: 'Ctrl',
controller: function() {
var $this = this;
$this.title = "bla";
AccountUserData.getUsers().then(function(users) {
$this.users = users;
});
}
}
}]);
My unit test looks like this:
describe("account-user-management directive tests", function () {
var oScope;
var oAccountUserManagementDirective;
var oAccountUserManagementServiceMock;
beforeEach(function () {
initMocks();
loadModule();
inject(function (_$rootScope_, _$compile_) {
compileDirective(_$rootScope_, _$compile_);
});
});
function initMocks() {
oAccountUserManagementServiceMock = {
getUsers: function () {
return {
then: jasmine.createSpy('then'),
}
}
};
}
function loadModule() {
module('account_user_management', function ($provide) {
$provide.service('AccountUserData', function () {
return oAccountUserManagementServiceMock;
});
});
}
function compileDirective($rootScope, $compile) {
oScope = $rootScope.$new();
oAccountUserManagementDirective =
$compile("<account-user-management></account-user-management>")(oScope);
oScope.$digest();
}
// THIS PASSES
it("should contain a 'title' property in the controller", function () {
expect(oScope.Ctrl.title).toBeDefined();
});
// THIS FAILS
it("should contain a 'users' property in the controller", function () {
expect(oScope.Ctrl.users).toBeDefined();
});
The unit test is failing due to the users property being undefined.
Any help is much appreciated, think the problem is due to the getUsers() function in the mocked service returning a promise (the then()) but not sure what I'm doing wrong.
Essentially, I want to be able to mock out the getUsers call to return some stubbed users for testing?
Aucun commentaire:
Enregistrer un commentaire