I'm trying to mock out this 'usermanager' provider in my controller test and I always end up getting this error
TypeError: Attempted to assign to readonly property.
at workFn (c:{...}/bower_components/angular-mocks/angular-mocks.js:2105)
Here is my controller:
angular.module('sasApp')
.controller('RegisterCtrl', function ($scope, usermanager) {
// variables
$scope.captcha = {};
// Get captcha from server
usermanager.getCaptcha().then(function (captcha) {
$scope.captcha = captcha;
}, function (reason) {
console.log('Failed: ' + reason);
});
});
And here is my test:
describe('Controller: RegisterCtrl', function () {
// load the controller's module
beforeEach(module('sasApp'));
var RegisterCtrl,
scope,
mockUsermanager;
mockUsermanager = {
getCaptcha: function () {
return {
test: 1
};
}
};
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
RegisterCtrl = $controller('RegisterCtrl', {
$scope: scope,
usermanager: mockUsermanager
});
}));
describe('Initialization', function () {
it('should have \'captcha\' object defined', function () {
expect(scope.captcha).toBeDefined();
});
});
});
The thing is, I didn't get this error until I tried setting up the mocking of the 'usermanager' provider. If I remove the line 'usermanager: mockUsermanager' in the beforeEach statement, then it all runs well.
Can you spot what is wrong ? Thanks in advance!
It might not be the best answer but I've made a Plnkr that does not reproduce the issue here: http://plnkr.co/GgUeKnsvxEHmD3Wcyocx?p=preview
A quick note: usermanager.getCaptcha()
has to return a promise otherwise the next part of test would blow up when it tries calling .then
. So I've just put in a bogus one in.
Let me know if you spot any differences (I don't know what angular version you're on for example). And I can see about updating
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.