Search code examples
angularjsunit-testingangular-mock

Angular-mocks error instantiating $rootScope


I have a very simple Mocha test (a copy of code that I have seen dozens of times) enter image description here

When I run it, angular mocks appears to successfully load the injected resources and all their dependencies and successfully makes it to line 9. When I step into the $rootScope.new() function it goes directly to the catch block. enter image description here

with e as "undefined is not a function"

enter image description here

I am not understanding what is going on here. The variable blockFns[i] is a function, or at least the Chrome debugger says it is. So I don't understand why the call to injector.invoke fails. Here is the stack trace. enter image description here


Solution

  • You need to inject $rootScope before you call $rootScope.$new() like so:

    var $rootScope;
    beforeEach(inject(function($injector){
      $rootScope = $injector.get('$rootScope');
    }));
    

    Updated

    Actually the issue was that it should be $rootScope.$new() instead of $rootScope.new().