angularjs karma无法在范围内找到函数

时间:2017-10-13 14:06:40

标签: javascript angularjs unit-testing karma-runner

我有一个AngularJS函数

$scope.goneHome = () => {
  filterSegmentStateService.resetPersistence();
};

我正在尝试进行单元测试。

/* eslint-disable */

/**
 * Test for campaign tags.
 */
describe('Header component', () => {
  // load the controller's module
  beforeEach(() => {
    angular.mock.module('keywordsApp');
  });

  var $componentController, scope, component, element, filterSegmentStateService;

  // Initialize the controller and a mock scope
  beforeEach(angular.mock.inject(function ($rootScope, _$componentController_, _filterSegmentStateService_) {
    scope = $rootScope.$new();
    filterSegmentStateService = _filterSegmentStateService_;
    $componentController = _$componentController_;
    component = $componentController('headerComponent', { $scope: scope });
  }));

  it('should run filterSegmentStateService function if goneHome function is selected', function () {
    console.log('TESTING 123');
    console.log(filterSegmentStateService);
    console.log(scope);
    console.log(scope.goneHome());
    expect(filterSegmentStateService.resetPersistence).toBeDefined();
  });
});

因此filterSegmentStateService显示在我的日志和scope中,但scope.goneHome()会返回undefined

所以我查看了scope并记录了以下内容:

Scope {
    $$childTail: null,
    $$childHead: null,
    $$nextSibling: null,
    $$watchers: null,
    $$listeners: Object {},
    $$listenerCount: Object {},
    $$watchersCount: 0,
    $id: 120,
    $$ChildScope: null,
    $parent: Scope {
        $id: 119,
        $$childTail: Scope {
            $$childTail: ...,
            $$childHead: ...,
            $$nextSibling: ...,
            $$watchers: ...,
            $$listeners: ...,
            $$listenerCount: ...,
            $$watchersCount: ...,
            $id: ...,
            $$ChildScope: ...,
            $parent: ...,
            $$prevSibling: ...,
            goneHome: ...,
            $ctrl: ...
        },
        $$childHead: Scope {
            $$childTail: ...,
            $$childHead: ...,
            $$nextSibling: ...,
            $$watchers: ...,
            $$listeners: ...,
            $$listenerCount: ...,
            $$watchersCount: ...,
            $id: ...,
            $$ChildScope: ...,
            $parent: ...,
            $$prevSibling: ...,
            goneHome: ...,
            $ctrl: ...
        },
        $$prevSibling: null,
        $$nextSibling: null,
        $$watchers: [..., ...],
        $parent: null,
        $$phase: null,
        $root: Scope {
            $id: ...,
            $$childTail: ...,
            $$childHead: ...,
            $$prevSibling: ...,
            $$nextSibling: ...,
            $$watchers: ...,
            $parent: ...,
            $$phase: ...,
            $root: ...,
            $$destroyed: ...,
            $$listeners: ...,
            $$listenerCount: ...,
            $$watchersCount: ...,
            $$isolateBindings: ...,
            $$asyncQueue: ...,
            $$postDigestQueue: ...,
            $$applyAsyncQueue: ...,
            gsGlobalVariables: ...,
            $$ChildScope: ...
        },
        $$destroyed: false,
        $$listeners: Object {
            $locationChangeSuccess: ...,
            $destroy: ...
        },
        $$listenerCount: Object {
            $locationChangeSuccess: ...,
            $destroy: ...
        },
        $$watchersCount: 2,
        $$isolateBindings: null,
        $$asyncQueue: [..., ..., ..., ...],
        $$postDigestQueue: [],
        $$applyAsyncQueue: [],
        gsGlobalVariables: Object {
            pageLoaded: ...,
            requestError: ...,
            windowStatusLoadMessage: ...
        },
        $$ChildScope: function ChildScope() { ...
        }
    },
    $$prevSibling: null,
    goneHome: function() { ...
    },
    $ctrl: HeaderController {
        $rootScope: Scope {
            $id: ...,
            $$childTail: ...,
            $$childHead: ...,
            $$prevSibling: ...,
            $$nextSibling: ...,
            $$watchers: ...,
            $parent: ...,
            $$phase: ...,
            $root: ...,
            $$destroyed: ...,
            $$listeners: ...,
            $$listenerCount: ...,
            $$watchersCount: ...,
            $$isolateBindings: ...,
            $$asyncQueue: ...,
            $$postDigestQueue: ...,
            $$applyAsyncQueue: ...,
            gsGlobalVariables: ...,
            $$ChildScope: ...
        },
        $scope: Scope {
            $$childTail: ...,
            $$childHead: ...,
            $$nextSibling: ...,
            $$watchers: ...,
            $$listeners: ...,
            $$listenerCount: ...,
            $$watchersCount: ...,
            $id: ...,
            $$ChildScope: ...,
            $parent: ...,
            $$prevSibling: ...,
            goneHome: ...,
            $ctrl: ...
        },
        filterSegmentStateService: filterSegmentStateService {
            pagination: ...,
            sort: ...,
            getFilterAll: ...,
            setCurrentPage: ...,
            getCurrentPage: ...,
            getPaginationState: ...,
            getSort: ...,
            groupView: ...,
            setLanguageFilter: ...,
            getLanguageFilter: ...,
            resetLanguageFilter: ...,
            getStatusFilter: ...,
            getThrottleFilter: ...,
            resetStatusFilter: ...,
            setFilterValues: ...,
            setTypesFilter: ...,
            getFilterOptionByLabel: ...,
            resetTypesFilter: ...,
            getTypesFilter: ...,
            setGroupsFilter: ...,
            getGroupsFilter: ...,
            toggleView: ...,
            getView: ...,
            isGroupView: ...,
            unselectGroups: ...,
            setDefaultListView: ...,
            selectGroup: ...,
            isGroupSelected: ...,
            groupSelected: ...,
            getSelectedGroup: ...,
            resetPersistence: ...
        }
    }
}

所以在内部,我确实看到了以下内容。

goneHome: function() { ...
},

为什么我无法使用此功能在我的控制器功能中运行filterSegmentStateService.resetPersistence()

0 个答案:

没有答案