Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS factory testing in Karma with Jasmine
    primarykey
    data
    text
    <p>I am trying to test my AngularJS app with Jasmine via Karma. I get this error (at least, this is the latest of them):</p> <pre><code>Uncaught TypeError: Cannot read property '$modules' of null at /Users/benturner/Dropbox/Code/galapagus/app/static/js/angular-mocks.js:1866 </code></pre> <p>From my karma.conf.js:</p> <pre><code>files: [ 'static/js/jquery.min.js', 'static/js/angular.min.js', 'static/js/angular-mocks.js', 'static/js/angular-resource.min.js', 'static/js/angular-scenario.js', 'static/js/angular-loader.min.js', 'static/js/momentous/ctrl_main.js', // contains all my app's code 'test/momentous.js' ], </code></pre> <p>Here is my test:</p> <pre><code>(function () { "use strict"; var controller = null; var scope = null; describe("Services", inject(function($rootScope, Moments) { var mockedFactory, moments, flag, spy; moments = [{name: 'test'}]; beforeEach(module('momentous', function($provide) { scope = $rootScope.$new(); $provide.value('$rootScope', scope); mockedFactory = { getList: function() { return moments; } }; spy = jasmine.createSpy(mockedFactory.getList); $provide.value('Moments', mockedFactory); })); it('should return moments from the factory service', function() { runs(function() { console.log(scope.getList); flag = false; setTimeout(function() { scope.getList(); flag = true; }, 500); }); waitsFor(function() { return flag; }, "The call is done", 750); runs(function() { expect(scope.moments).toEqual([{name: 'test'}]); expect(spy).toHaveBeenCalled(); }); }); })); }()); </code></pre> <p>So what I am trying to do is mock my factory service and check that it is returning an array of objects and setting them to a variable in $scope.</p> <p>There's an async call in there too so I had to use runs() and waitsFor().</p> <p>I still don't understand how I am injecting my $scope so that I can test against it, and with angular-mocks.js now giving me an error I feel I'm getting further away from solving this, not closer.</p> <p>I cobbled this together from various docs, guides, and stackoverflow answers. Any guidance? Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload