Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度单元测试,SpyOn独立功能_Javascript_Angularjs_Unit Testing_Jasmine - Fatal编程技术网

Javascript 角度单元测试,SpyOn独立功能

Javascript 角度单元测试,SpyOn独立功能,javascript,angularjs,unit-testing,jasmine,Javascript,Angularjs,Unit Testing,Jasmine,我正在使用Jasmine测试Angular应用程序,并希望测试在调用控制器的ready()函数时是否调用控制器中的getItem()函数 ---控制器--- var vm = this; vm.items = []; $ionicPlatform.ready(ready); function ready() { vm.items.push(getItem()); function getItem(){ var item = //do stuff to

我正在使用Jasmine测试Angular应用程序,并希望测试在调用控制器的ready()函数时是否调用控制器中的getItem()函数

---控制器---

var vm = this;
vm.items = [];
$ionicPlatform.ready(ready);

function ready() {
     vm.items.push(getItem());

     function getItem(){
          var item = //do stuff to get item;
          console.log('getItem called');
          return item;
     }
}
describe('Controller', function(){

     //--- Load app with dependencies and module to test code omitted.

     beforeEach(function(){
          //How do I spy on getItem() to test that it was called?
          //I've tried getItem = jasmine.createSpy()
          //I've tried spyOn(window, 'getItem')
     }

     //--- Initialize the controller and a mock scope code omitted.

     beforeEach(function(done){
          $ionicPlatform.ready(function(){
               done();
          });
     });

     it('getItem function should get called', function(){
          expect(getItem).toHaveBeenCalled();

          //--- Note, getItem does not get called according to the expect statement, 
          //--- but does output 'getItem called' to the terminal when running the test.
     });

});
---规范---

var vm = this;
vm.items = [];
$ionicPlatform.ready(ready);

function ready() {
     vm.items.push(getItem());

     function getItem(){
          var item = //do stuff to get item;
          console.log('getItem called');
          return item;
     }
}
describe('Controller', function(){

     //--- Load app with dependencies and module to test code omitted.

     beforeEach(function(){
          //How do I spy on getItem() to test that it was called?
          //I've tried getItem = jasmine.createSpy()
          //I've tried spyOn(window, 'getItem')
     }

     //--- Initialize the controller and a mock scope code omitted.

     beforeEach(function(done){
          $ionicPlatform.ready(function(){
               done();
          });
     });

     it('getItem function should get called', function(){
          expect(getItem).toHaveBeenCalled();

          //--- Note, getItem does not get called according to the expect statement, 
          //--- but does output 'getItem called' to the terminal when running the test.
     });

});

不幸的是,您遇到了使用Jasmine进行Javascript单元测试的一个基本限制——您只能在某些对象上公开
spyOn
方法。如果有一个函数是另一个函数的内部函数,并且没有公开,则不能直接测试它

但是,您有两种选择:

  • 以一种可以窥探的方式公开函数(通常作为测试任何角度组件的方法)
  • 间接测试它
  • 第一种可能是相对不言而喻的,但后一种可能有点令人困惑。基本上,如果调用了
    getItems
    函数,则无法直接测试,但该函数可能有它调用的下游方法或它更改的值,您可以测试。例如,您可以在调用
    ready
    后测试
    vm.items.push
    是否更大,或者您可以
    spyOn(console.log)
    expect(console.log).toHaveBeenCalledWith('getItem called')

    你可以在互联网上找到这两种方法的论据——我倾向于选择第二种方法,因为我不喜欢仅仅为了可测试性而进行重构,但许多人会认为,为了可测试性而进行重构通常会产生更好的代码。这是你的选择。希望这有帮助