$ rootscope.digestは、アンギュラ指向単位テストで「これ以上要求は予想されません」

StackOverflow https://stackoverflow.com//questions/21003221

  •  20-12-2019
  •  | 
  •  

質問

アクセス許可がKOの場合は、いくつかの権限をチェックし、要素を削除するディレクティブをコーディングしました。

私はそれを単体テストしたいです、しかし... hem、私はこの単純なテスト作業をするために私の頭を再びぶつけます。

$ rootscope.digest()を使用してHTMLをコンパイルします。この関数を呼び出すとき、Appularは私のアプリのメインページをロードしようとします、そして、私は恐ろしい「もうそれ以上要求は予想されない」エラーです。

だからここにテストがあります:

describe('Unit testing permission-needed', function() {
    var $compile;
    var $rootScope;
    var $httpBackend;

    // Load the myApp module, which contains the directive
    beforeEach(module('app'));

    beforeEach(angular.mock.module('ngMockE2E'));

    beforeEach(inject(function(_$compile_, _$rootScope_, $injector) {
        $compile = _$compile_;
        $rootScope = _$rootScope_;
        $httpBackend = $injector.get('$httpBackend'); // not sur if I can inject it like others services ?
        $httpBackend.whenGET('app/login/login.tpl.html').passThrough(); // this doesn't seem to work

    }));



    it('should replace the element with the appropriate content', function() {



        // Compile a piece of HTML containing the directive
        var element = $compile("<div permission-needed><span>Some content goes here</span></div>")($rootScope);
        $rootScope.$digest(); // BAM => "no more request expected"

        // Do the test here
        // expect(....);
    });
});
.

私が使えば注意してください ···(「ここでのいくつかのHTML」)。 の代わりに .passthrough()うまく機能します。

ありがとうございました。

役に立ちましたか?

解決

常に自分自身に答える:

$ NEW()ROOTSCOPE、テストは渡されます:

$rootScope = _$rootScope_.$new();
.

これが誰かに助けてくれることを願っています

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top