jviotti jviotti - 5 months ago 593
Javascript Question

TypeError: Attempted to assign to readonly property

I'm facing this error when writing a test for a directive (using

generator-angular-module
):

src/capitalize.js:

'use strict';

angular.module('jviotti.string', []).filter('capitalize', function() {

return function(input) {
return input.replace(/\w\S*/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
};

});


test/spec/capitalize.js:

'use strict';

describe('Filter: capitalize', function () {

// load the controller's module
beforeEach(module('jviotti.string'));

var capitalize;
beforeEach(inject(function($filter) {
capitalize = $filter('capitalize');
}));

it('should capitalize a string', function () {
expect(capitalize('hello')).toBe('Hello');
});

});


However, when running Karma with PhantomJS I get:

PhantomJS 1.9.2 (Mac OS X) Filter: capitalize should capitalize a string FAILED
TypeError: Attempted to assign to readonly property.
at workFn (/Users/jviotti/Projects/angular-string/bower_components/angular-mocks/angular-mocks.js:2107)
TypeError: 'undefined' is not a function (evaluating 'capitalize('hello')')
at /Users/jviotti/Projects/angular-string/test/spec/capitalize.js:14
PhantomJS 1.9.2 (Mac OS X): Executed 2 of 2 (1 FAILED) (0.1 secs / 0.017 secs)


What am I missing?

Answer

Change in karma.conf.js

  logLevel: config.LOG_INFO,

to

  logLevel: config.LOG_DEBUG,

and PhantomJS to Firefox, then You will get more verbose error message