不确定 AngularJS 单元测试中的错误

unsure of errors in angularjs unit testing

本文关键字:错误 单元测试 AngularJS 不确定      更新时间:2023-09-26
我正在尝试在AngularJs

中对控制器进行单元测试,并且我正在遵循AngularJs文档作为指导。 https://docs.angularjs.org/guide/controller

在指南(在上面的链接中)中,他们在控制器中有此代码

$scope.spice = "habanero";

和这个代码在测试中

it('should set the default value of spice', function() {
      expect($scope.spice).toBe('habanero');
    });

在我正在测试的控制器中,有一些这样的代码:

$scope.sessionViewModel = session;

在我的测试中,我写了这个:

it('should set the default value of sessionViewModel', function(){
            expect($scope.sessionViewModel).toBe(session);
        });

然而,我收到一个错误,说TypeError cannot read property 'sessionViewModel' of undefined

不太确定我做错了什么

整个测试文件:

describe('forgotPasswordCtrl function', function() {
    describe('forgotPasswordCtrl', function(){
    var $scope;
    var returnMsg = 'Forgot password response message';
    var returnMsgTwo = '/web/tfgm_customer';
    beforeEach(module('forgotPasswordApp'));

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

        it('returns a -forgot password response- message', function(){
            expect($scope.globalError).toEqual(returnMsg);
        });
        it('assigns a url depending on a customer match happens', function(){
            expect(window.location.assign).toEqual(returnMsgTwo);
            });
        it('should set the default value of sessionViewModel', function(){
            expect($scope.sessionViewModel).toBe(session);
        });
      });
    });

尝试更改

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

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