AngularJs单元测试 - 检查 - QUOT;初始化"函数被调用 [英] AngularJs unit test - Check if "Init" function was called

查看:202
本文介绍了AngularJs单元测试 - 检查 - QUOT;初始化"函数被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用茉莉花作为testframework和我下面的控制器我想测试。我有永诺为这个控制器一个init()函数在那里我把我会调用。

现在我想测试,如果当控制器初始化初始化函数被调用。

 函数UnitTestsCtrl(){
   VAR认为这=;
   this.Init();
}UnitTestsCtrl.prototype.Init =功能(){
    VAR认为这=;
    //一些动作
}angular.module(unitTestsCtrl,[])
       .controller(unitTestsCtrl,UnitTestsCtrl);

但我不能检查,如果初始化函数被调用控制器创建。我知道,因为间谍设置在创建后的初始化函数我的示例不起作用。

 描述('测试控制器:UnitTestsCtrl',函数(){
VAR CTRL;   beforeEach(函数(){
         模块('app.main');
         注(函数($控制器){
            CTRL = $控制器('unitTestsCtrl',{});
         });
   });   它('初始化被称为在控制器初始化',函数(){
       //那不是工作
       spyOn(CTRLINIT');
       期待(ctrl.Init).toHaveBeenCalled();
   });
});

解决方案:

这是在beforeEach功能的原始原型创建间谍

  beforeEach(函数(){
     模块('app.main');     spyOn(UnitTestsCtrl.prototype,'初始化');     注(函数($控制器){
        CTRL = $控制器('unitTestsCtrl',{});
     });
 }); 它('初始化被称为在控制器初始化',函数(){
        期待(ctrl.Init).toHaveBeenCalled();
 });


解决方案

事情是这样的,你不能,你真的不用为好。你不能因为你所呼叫的原因的init()控制器上的构造,即在实例,当你调用这恰好 $控制器服务实例在测试控制器。那么,你是太晚了设立间谍。你可能并不需要,因为如果控制器被实例化init方法会被称为是肯定的。但是怎么过,如果你正在里面初始化任何特定服务/依赖电话,您可以窥探那些嘲笑和设置的期望。

您的期望说:服务呼叫执行,以便创建该服务的间谍,并设置期望

例如:

  VAR为myService = jasmine.createSpyObj('为myService',['someCall']);
   myService.someCall.and.returnValue($ q.when(someObj中));
   // ...
  CTRL = $控制器('unitTestsCtrl',{'为myService':为myService});

和设置为myService的方法someCall的期望。

 预期(myService.someCall).toHaveBeenCalled();

如果你真的想在间谍的init ,那么你就需要有机会获得 UnitTestsCtrl 构造中的规格,你需要实例之前设置窥视它的原型方法的init

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.

Now I want to test if the Init function was called when the controller was initialized.

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. I know my example doesn't work because the spy is set on the Init function after creation.

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(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, i.e on instantiation, which happens when you call $controller service to instantiate the controller in your test. 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. But how ever if you are making any specific service/dependency calls inside init, you can spy on those mocks and set up expectations.

Your expectation says: Service Call executed so create a spy for that service and set up expectation.

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.

 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.

这篇关于AngularJs单元测试 - 检查 - QUOT;初始化"函数被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆