AngularJS 在 Karma 测试中设置作用域变量

AngularJS set scope variables in Karma test

我正在尝试设置 Karma 单元测试,在我的测试中我想设置一个范围变量,以便我可以 运行 测试。我收到错误 Cannot set 属性 'expandedSeries' of undefined.

下面是我的代码。我做错了什么?

describe('FormController', function () {
    beforeEach(module('userFormApp'));
    var $controller;
    var $rootScope;

    beforeEach(inject(function (_$controller_, _$rootScope_) {
    $controller = _$controller_;
    $rootScope = _$rootScope_;
    }));

    describe('$scope.getImageSrc', function () {
    var $scope, controller;

    beforeEach(function () {
        $scope = $rootScope.$new();           
        controller = $controller('FormController', { $scope: $scope});
    });

    $scope.expandedSeries = 1;

    it('sets variables ', function () {
        expect($scope).toBeDefined();
        expect($scope.expandedSeries).toBeDefined();
        expect($scope.expandedSeries).toEqual(1);
    });
});

在每个之前实例化变量,以便测试用例在启动时可以获取它。

describe('$scope.getImageSrc', function () {
var $scope, controller;

beforeEach(function () {
    $scope = $rootScope.$new();           
    controller = $controller('FormController', { $scope: $scope});
$scope.expandedSeries = 1;
});

    it('sets variables ', function () {
    expect($scope).toBeDefined();
    expect($scope.expandedSeries).toBeDefined();
    expect($scope.expandedSeries).toEqual(1);
});

});