简体   繁体   English

如何在茉莉花测试中模拟servicecall?

[英]How to mockout servicecall in jasmine test?

I created an angular custom directive with a service called scriptingService in it. 我创建了一个角度自定义指令,其中包含一个名为scriptingService的服务。 The goal is to mock the service call out with a spyOn. 目的是使用spyOn模拟服务调用。 This is part of the test: 这是测试的一部分:

 beforeEach(inject(function ($rootScope, $compile,_scriptingService_) {
      scope = $rootScope.$new();
      scope.row = 1;

      scriptingService = _scriptingService_;
      Restangular = _Restangular_;

      spyOn(Restangular, 'all').and.callThrough();

      spyOn(scriptingService, 'getScript').and.callThrough();

      element = angular.element('<ul id="rows" ui-list="row">');
      $compile(element)(scope);
      scope.$digest();
    }));

This is the directive code: 这是指令代码:

.directive('uiList', [
    function(scriptingService) {
        return {
            scope: {
                lengthModel: '=uiList'
            },
            link: function(scope, elm, attrs) {
                scope.$watch('lengthModel', function(newVal) {
                    scope.test=2;
                    console.log('kut');

                    scriptingService.getScript(request).then(function(scripts){
                        scope.scripts = scripts;
                    });

                });
            }
        };
    }
]);

However I am getting an error: 但是我得到一个错误:

RestangularProvider <- Restangular <- scriptingService

How can I mock the scriptingService and make sure the method was called? 如何模拟sc​​riptingService并确保调用了该方法? Plunker ref: http://plnkr.co/edit/CDc7EV?p=preview 柱塞参考: http ://plnkr.co/edit/CDc7EV?p = preview

There are a couple of issues here: 这里有几个问题:

  1. Like the comment said above, you need to use a newer version of jasmine for callThrough functionality, but also Restangular requires underscore, so that needs to be referenced as well in your html file. 就像上面说的评论一样,您需要使用较新版本的jasmine来实现callThrough功能,而且Restangular需要使用下划线,因此您的html文件中也需要引用它。

  2. Also, the dependency injection needed to be fixed for your directive, because it was not able to inject scriptingService properly, which never allowed it to call scriptingService.getScript since scriptingService was undefined . 另外,依赖项注入需要针对您的指令进行修复,因为它不能正确注入scriptingService ,因为scriptingServiceundefined ,所以它从未允许它调用scriptingService.getScript The issue is that 问题是

     [ function(scriptingService) {}] 

    will not work to inject scriptingService into uiList . scriptingService注入uiListuiList It just ends up being undefined . 最终结果是undefined

    It needs to either be 它必须是

     ['scriptingService', function(scriptingService) {}] 

    OR 要么

     function(scriptingService) {} 

    For reference: https://docs.angularjs.org/guide/di#dependency-annotation 供参考: https : //docs.angularjs.org/guide/di#dependency-annotation

  3. request is never defined when it's gets used in this code, I'm not sure what it's supposed to be. 在此代码中使用该request时,它永远不会定义,我不确定它应该是什么。

     scriptingService.getScript(request).then(function(scripts) { scope.scripts = scripts; }); 
  4. After fixing these issues, the test was still failing, saying Error: Unexpected request: GET /scripting. 解决了这些问题后,测试仍然失败,并显示错误:意外的请求:GET / scripting。

    I was able to fix this by injecting $httpBackend (which is a service for mocking http calls in angular) into the tests and expecting a request. 我可以通过将$httpBackend (这是一个用于$httpBackend http调用的服务)并测试中解决此问题的方法。 So I changed 所以我改变了

     inject(function ($rootScope, $compile,_scriptingService_,_Restangular_) 

    TO

     inject(function ($rootScope, $compile,_scriptingService_,_Restangular_, $httpBackend) 

    and added this 并添加了这个

     $httpBackend.expectGET("/scripting").respond([]); 

    after the spyOn 's, then the test passed. spyOn之后,测试通过了。 I'm not sure exactly what this http call is supposed to return, but this is just an example of how you could test it. 我不确定此http调用应该返回什么,但这只是如何测试它的示例。

    For reference: 以供参考: https://docs.angularjs.org/api/ngMock/service/ $httpBackend https://docs.angularjs.org/api/ngMock/service/ $ httpBackend

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

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