0

コードをフィドルに入れて、必要に応じて簡単に更新して「操作」できるようにします。

describe('PlayersListCtrl', function() {  // Jasmine Test Suite

    beforeEach(module('wc2014App'));

    var ctrl, scope, $httpBackend;

    beforeEach(inject(function($controller, $rootScope) {

        scope = $rootScope.$new();

        ctrl = $controller('PlayersListCtrl', {
            $scope: scope
        });
    }));

    it('should have an empty player array', function() { 
        expect(scope.players.length).toBe(0);
    });

    describe('PlayersListCtrl', function() {
        var $httpBackend, $rootScope, createController;

        beforeEach(inject(function($injector) {
            $httpBackend = $injector.get('$httpBackend');
            $httpBackend.when('GET', '../app/stubs/players.json').respond(
                {userId: 'userX'}, 
                {'A-Token': 'xxx'});

            $rootScope = $injector.get('$rootScope');
            var $controller = $injector.get('$controller');

            createController = function() {
                return $controller('PlayersListCtrl', {'$scope' : $rootScope });
            };
        }));

        afterEach(function() {
            $httpBackend.verifyNoOutstandingExpectation();
            $httpBackend.verifyNoOutstandingRequest();
        });

        it('should fetch authentication token', function() {
            $httpBackend.expectGET('../app/stubs/players.json');
            var controller = createController();
            $httpBackend.flush();
        });
    });
});

残りは非常に冗長であるため、フィドルにあります:http://jsfiddle.net/tLte2/

基本的に最初のテストは成功し、難しいテストではありませんが、2 番目のテストは JSON スタブに依存し、次のようなエラーが発生します。 PhantomJS 1.9.7 (Mac OS X) PlayersListCtrl PlayersListCtrl should fetch authentication token FAILED Error: No pending request to flush !

httpBackendこの $ stiffがどのように機能するかを把握することはできません。それを起動して結果をコントローラーのスコープに設定することは可能でなければなりませんか?

--edit 基本的にすべてが完璧に配線されており、いくつかの簡単なテストを問題なく実行できますが、そこに JSON スタブ データを取得するのは面倒なようです。回避策として、コントローラ スコープの JSON に記述された配列を次のように定義するだけcontroller.players = ['one','two','three',..... etc ......] です。その$httpBackendようなものは、修正するのがそれほど難しくないはずですよね?

4

0 に答える 0