Karma を介して Jasmine で AngularJS アプリをテストしようとしています。このエラーが発生します(少なくとも、これは最新のものです):
Uncaught TypeError: Cannot read property '$modules' of null
at /Users/benturner/Dropbox/Code/galapagus/app/static/js/angular-mocks.js:1866
私のkarma.conf.jsから:
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'
],
これが私のテストです:
(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();
});
});
}));
}());
だから私がしようとしているのは、ファクトリ サービスをモックし、それがオブジェクトの配列を返し、それらを $scope の変数に設定していることを確認することです。
そこにも非同期呼び出しがあるため、runs() と waitsFor() を使用する必要がありました。
$scope を挿入してテストできるようにする方法をまだ理解していません。 angular-mocks.js でエラーが表示されるようになり、これを解決するのではなく、解決から遠ざかっているように感じます。
さまざまなドキュメント、ガイド、およびスタックオーバーフローの回答からこれをまとめました。ガイダンスはありますか?ありがとう。