简体   繁体   English

AngularJs单元测试-检查是否调用了“ Init”功能

[英]AngularJs unit test - Check if “Init” function was called

I'm using jasmine as testframework and I've the following Controller I want to test. 我使用茉莉花作为测试框架,下面是我要测试的控制器。 And I allways have a Init() function where I place my initialization calls for this Controller. 而且我一直都有一个Init()函数,在该函数中我对该控制器进行初始化调用。

Now I want to test if the Init function was called when the controller was initialized. 现在,我想测试初始化​​控制器时是否调用了Init函数。

function UnitTestsCtrl() {
   var that = this;
   this.Init();
}

UnitTestsCtrl.prototype.Init = function() {
    var that = this;
    //Some more Stuff
}

angular.module("unitTestsCtrl", [])
       .controller("unitTestsCtrl", UnitTestsCtrl);

But I was not able to check if the Init function was called on controller creation. 但是我无法检查是否在创建控制器时调用了Init函数。 I know my example doesn't work because the spy is set on the Init function after creation. 我知道我的示例不起作用,因为间谍是在创建后在Init函数上设置的。

describe('Tests Controller: "UnitTestsCtrl"', function() {
var ctrl;

   beforeEach(function() {
         module('app.main');
         inject(function ($controller) {
            ctrl = $controller('unitTestsCtrl', {});
         });
   });

   it('Init was called on Controller initialize', function () {
       //thats not working
       spyOn(ctrl, 'Init');
       expect(ctrl.Init).toHaveBeenCalled();
   });
});

Solution: 解:

Create the spy on the Original Prototype in the beforeEach function 在beforeEach函数中在原始原型上创建间谍

 beforeEach(function() {
     module('app.main');

     spyOn(UnitTestsCtrl.prototype, 'Init');

     inject(function ($controller) {
        ctrl = $controller('unitTestsCtrl', {});
     });
 });

 it('Init was called on Controller initialize', function () {
        expect(ctrl.Init).toHaveBeenCalled();
 });

The way it is, you cannot and you really do not need to as well. 这样,您就不能也确实不需要。 The reason you cannot is because you are calling init() on the controller constructor, ie on instantiation, which happens when you call $controller service to instantiate the controller in your test. 之所以不能,是因为您正在控制器构造函数上调用init() ,即在实例化上,这种情况发生在调用$controller服务以实例化测试中的控制器时。 So you are setting up spy too late. 因此,您设置间谍太迟了。 You probably do not need to, because if the controller is instantiated init method would have been called for sure. 您可能不需要这样做,因为如果实例化了控制器,则肯定会调用init方法。 But how ever if you are making any specific service/dependency calls inside init, you can spy on those mocks and set up expectations. 但是,如果您在init内进行任何特定的服务/依赖关系调用,则可以监视这些模拟并建立期望。

Your expectation says: Service Call executed so create a spy for that service and set up expectation. 您的期望值是: Service Call executed因此为该服务创建一个间谍并设置期望值。

example: 例:

   var myService = jasmine.createSpyObj('myService', ['someCall']);
   myService.someCall.and.returnValue($q.when(someObj));
   //...


  ctrl = $controller('unitTestsCtrl', {'myService':myService});

and set the expectation on the method someCall of myService. 并设置对myService的someCall方法的期望。

 expect(myService.someCall).toHaveBeenCalled();

If you really want to spy on init , then you would need to have access to UnitTestsCtrl constructor in the spec and you would need to set spy on its prototype method init before instantiating. 如果您真的想监视init ,则需要在规范中访问UnitTestsCtrl构造函数,并且需要在实例化之前对其原型方法init进行监视。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM