Javascript AngularJS使用jasmine和spyOn测试控制器时出现错误';方法未定义';

Javascript AngularJS使用jasmine和spyOn测试控制器时出现错误';方法未定义';,javascript,angularjs,unit-testing,testing,jasmine,Javascript,Angularjs,Unit Testing,Testing,Jasmine,因此,我正在使用Jasmine为angularJS中的应用程序编写单元测试 我有一个带有“init”方法的控制器,该方法调用“secondMethod”和“thirdMethod” 我想用jasmine spyOn测试是否正确调用了“secondMethod” 我的控制器如下所示: function init() { secondMethod().then(function () { thirdMethod(); }); } init(); function

因此,我正在使用Jasmine为angularJS中的应用程序编写单元测试

我有一个带有“init”方法的控制器,该方法调用“secondMethod”和“thirdMethod”

我想用jasmine spyOn测试是否正确调用了“secondMethod”

我的控制器如下所示:

function init() {
    secondMethod().then(function () {
        thirdMethod();
    });
}

init();

function secondMethod(){
    //do something
}

function thirdMethod(){
    //do something
}
describe("nameOfTheController", function () {
var $rootScope,
    $controller,
    $scope,
    controller;

beforeEach(function () {

    angular.mock.module("myModule");

    inject(function (_$controller_, _$rootScope_) {
        $rootScope = _$rootScope_;
        $controller = _$controller_;
        $scope = $rootScope.$new();
        controller = $controller('nameOfTheController', {
            '$scope': $scope
        });
    });
});

describe("init", function(){
    it('should run secondMethod', function(){
        spyOn(controller, 'secondMethod');
        expect(controller.secondMethod).toHaveBeenCalled();
    });
    it('should run thirdMethod', function(){
        spyOn(controller, 'thirdMethod');
        expect(controller.thirdMethod).toHaveBeenCalled();
    });
我的测试文件如下所示:

function init() {
    secondMethod().then(function () {
        thirdMethod();
    });
}

init();

function secondMethod(){
    //do something
}

function thirdMethod(){
    //do something
}
describe("nameOfTheController", function () {
var $rootScope,
    $controller,
    $scope,
    controller;

beforeEach(function () {

    angular.mock.module("myModule");

    inject(function (_$controller_, _$rootScope_) {
        $rootScope = _$rootScope_;
        $controller = _$controller_;
        $scope = $rootScope.$new();
        controller = $controller('nameOfTheController', {
            '$scope': $scope
        });
    });
});

describe("init", function(){
    it('should run secondMethod', function(){
        spyOn(controller, 'secondMethod');
        expect(controller.secondMethod).toHaveBeenCalled();
    });
    it('should run thirdMethod', function(){
        spyOn(controller, 'thirdMethod');
        expect(controller.thirdMethod).toHaveBeenCalled();
    });
正如您所看到的,我在beforeach中注入了控制器,但我得到了一个错误,即没有定义方法“secondMethod”和“thirdMethod”,我不太清楚为什么

我也尝试过做如下的事情,但没有效果

控制器:

var vm = this;
vm.init = function() {
    vm.secondMethod().then(function () {
        vm.thirdMethod();
    });
}

vm.init();

vm.secondMethod = function(){
    //do something
}

vm.thirdMethod = function(){
    //do something
}
测试文件:

describe("nameOfTheController", function () {
var $rootScope,
    $controller,
    $scope,
    controller;

beforeEach(function () {

    angular.mock.module("myModule");

    inject(function (_$controller_, _$rootScope_) {
        $rootScope = _$rootScope_;
        $controller = _$controller_;
        $scope = $rootScope.$new();
        controller = $controller('nameOfTheController', {
            '$scope': $scope
        });
    });
});

describe("init", function(){
    it('should run secondMethod', function(){
        spyOn(controller, 'secondMethod');
        expect(controller.secondMethod).toHaveBeenCalled();
    });
    it('should run thirdMethod', function(){
        spyOn(controller, 'thirdMethod');
        expect(controller.thirdMethod).toHaveBeenCalled();
    });
有人知道为什么第二个和第三个方法没有定义吗

编辑:

第二个和第三个方法在前缀为“vm”时返回undefined的原因是在定义第二个和第三个方法之前调用了init函数

将init的调用移动到第二个和第三个方法定义之下解决了这个问题。现在我遇到的问题是间谍希望调用该方法,但却没有调用它

var vm = this;
vm.init = function() {
    vm.secondMethod().then(function () {
        vm.thirdMethod();
    });
}

vm.secondMethod = function(){
    //do something
}

vm.thirdMethod = function(){
    //do something
}

vm.init();

控制器在
beforeach()
中启动,即当您
init()
=>
seconMethod()
时,而您仅在
it()
块中监视它

另一方面,您以前不能进行间谍活动,因为您没有控制器对象

在我看来,解决方案是修改代码并明确调用init():

it('should run secondMethod', function() {
    spyOn(controller, 'secondMethod');
    expect(controller.secondMethod).not.toHaveBeenCalled();
    controller.init();
    expect(controller.secondMethod).toHaveBeenCalled();

});

您是否尝试过定义像
$scope.secondMethod=function(){}
这样的函数?