I'm just starting to unit test my app, and have successfully gotten one test suite to run, yeah! I moved on to start testing a second controller, and as soon as the test runner (karma) hits an inject
block, the test fails. Comment it out, and the tests pass again. Can't for the life of me figure out what might be going on. Very basic karma config:
The controller looks like:
angular.module('somemodule')
.controller('SomeCtrl', SomeCtrl);
function SomeCtrl($rootScope, Notification) {
$rootScope.$on('someapp.apiLimitExceeded', (evt, err) => {
Notification.error({
message: err.data.message
});
});
}
The first test looks like:
describe('SomeCtrl', () => {
let $rootScope, controller, notification;
beforeEach(() => {
module('mymodule');
inject(($injector) => {
$rootScope = $injector.get('$rootScope');
notification = $injector.get('Notification');
spyOn(notification, 'error');
controller = $injector.get('$controller')('SomeCtrl', {
$rootScope: $rootScope,
Notification: notification
});
});
});
describe('Initialization', () => {
it('Should register a $rootScope listener for mymodule.apiLimitExceeded', () => {
const args = {data: {message: 'dis an errr'}};
$rootScope.$emit('mymodule.apiLimitExceeded', args);
expect(notification.error).toHaveBeenCalled();
expect(notification.error).toHaveBeenCalledWith(args.data);
expect(notification.error.calls.count()).toBe(1);
});
});
});
This test passes, as expected. If I literally copy and paste the contents of this file into another test file, without changing anything, the tests start failing. Removing the second test file makes the original test start passing. I must be missing something about how karma is running these angular tests, or some tearDown I'm not performing, but I can't see what that might be. Any ideas? The error I get, for reference is:
TypeError: Cannot read property '1' of null
at Function.fb.$$annotate (/Users/greg/projects/someapp/client/build/js/deps.js:7451:410)
at Function.angular.injector.$$annotate (/Users/greg/projects/someapp/client/node_modules/angular-mocks/angular-mocks.js:2615:36)
at e (/Users/greg/projects/someapp/client/build/js/deps.js:7298:368)
at Object.invoke (/Users/greg/projects/someapp/client/build/js/deps.js:7299:86)
at Object.workFn (/Users/greg/projects/someapp/client/node_modules/angular-mocks/angular-mocks.js:2965:20)
at window.inject.angular.mock.inject (/Users/greg/projects/someapp/client/node_modules/angular-mocks/angular-mocks.js:2934:42)
at Object.beforeEach (/Users/greg/projects/someapp/client/test/test.again.someapp.controller.js:7:5)
Error: Declaration Location
at window.inject.angular.mock.inject (/Users/greg/projects/someapp/client/node_modules/angular-mocks/angular-mocks.js:2927:25)
at Object.beforeEach (/Users/greg/projects/someapp/client/test/test.again.someapp.controller.js:7:5)
TypeError: Cannot read property '$emit' of undefined
at Object.it (/Users/greg/projects/someapp/client/test/test.again.someapp.controller.js:23:17)
Aucun commentaire:
Enregistrer un commentaire