3

私はすでにこの投稿(および他の投稿) を読みましたが、この単純な単体テストを機能させることができません。Jasmineのバージョン2を使用しています。私の工場はとてもシンプルです:

angular.module('myApp')
    .factory('detectPath', function ($location, $rootScope) {
        'use strict';
        var locationPath = $location.path()
        function getPath () {
            if (locationPath === '/') {
                locationPath = 'home';
            } else {
                locationPath = '';
            }
            $rootScope.path = locationPath;
        }
        getPath();
        return locationPath;
    });

そして、私の単体テストは同じくらい簡単です:

'use strict';
describe('Factory: detectPath', function () {
    var detectPath, $rootScope, $location;

    beforeEach(module('myApp'));
    beforeEach(inject(function (_detectPath_, _$rootScope_, _$location_) {
        detectPath = _detectPath_;
        $rootScope = _$rootScope_;
        $location = _$location_;
        spyOn($location, 'path').and.returnValue('/');
    }));

    it('should return pathName', function ($location) {
        expect($rootScope.path).toBe('home');
    });
});

これはテストに合格しません( false が「ホーム」になるとエラーが発生します)。

私が間違っていることは何ですか?spyOn が呼び出されたことを確認する方法はありますか (1 回だけ)?

4

1 に答える 1