6

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 でエラーが表示されるようになり、これを解決するのではなく、解決から遠ざかっているように感じます。

さまざまなドキュメント、ガイド、およびスタックオーバーフローの回答からこれをまとめました。ガイダンスはありますか?ありがとう。

4

1 に答える 1

10

私はまた、この正確なエラーを取得して立ち往生しています。私のコードは、プロバイダーをテストしようとしているところと似ているので、モジュールを呼び出して、プロバイダーを構成する関数を渡します。

解決済み:

この問題は、「inject」を呼び出してデリゲートを「describe」メソッドに返すことが原因であることがわかりました。inject を使用してデリゲートを「it」に戻すことしかできません。

例えば:

describe('something', inject(function(something) {}));  // will throw the $module is null error

しかし、これはうまくいきます:

it('something', inject(function(something) {}));  // works :)
于 2013-10-04T18:14:05.633 に答える