简体   繁体   English

如何使用茉莉花模拟对JS异步进行单元测试

[英]How to unit test js asynch with jasmine mocking

I am fairly new to JS and I was having some issues with testing asynch methods with jasmine mocking. 我对JS相当陌生,在使用茉莉花模拟测试异步方法时遇到一些问题。

Here is the class I want to test: 这是我要测试的课程:

 test.factory('service', ['$http', '$q', function($http, $q) {
     var _func = function(varB, successCallback, errorCallback) {

         //Sanity check on input
         if (isNaN(varB)) {
             errorCallback('varB has to be a number');
             return;
         }

         if (parseInt(varB) < 0) {
             errorCallback('varB count has to be positive');
             return;
         }

         $http.get('http://www.test.com').then(function(response) {
             var data = angular.copy(response.data);
             if (successCallback) {
                 successCallback(data);
             }
         }, function(errorResponse) {
             if (errorCallback) {
                 errorCallback(errorResponse.data);
             }
         });
     };

     return {
         func: function(varB) {
             return $q(function(resolve, reject) {
                 _func(varB, resolve, reject);
             });
         }
     };
 }]);

One of the tests: 测试之一:

 beforeEach(inject(function($injector) {
     service = $injector.get(
         'service');
     $httpBackend = $injector.get('$httpBackend');
 }));

 it('should use the rejection handler if varB is in invalid format',
     function() {
         var successHandler = jasmine.createSpy('success');
         var failHandler = jasmine.createSpy('fail');
         service.func('abc').then(successHandler, failHandler);
         expect(successHandler).not.toHaveBeenCalled();
         expect(failHandler).toHaveBeenCalledWith('varB has to be a number');
     }
 );

The line in the test: expect(failHandler).toHaveBeenCalledWith('varB has to be a number'); 测试中的行: expect(failHandler).toHaveBeenCalledWith('varB has to be a number'); never gets executed for some reason. 由于某种原因永远不会执行。 I even tried putting debug statements in the service class to see if the methods were reached and they were getting reached. 我什至尝试将调试语句放入服务类中,以查看是否已到达方法以及是否已到达方法。 But the mock was not able to detect that the callback was called with the given argument. 但是该模拟无法检测到使用给定参数调用了回调。

Would be great to get some insights! 希望能获得一些见解! Thanks! 谢谢!

Ok I found out the solution. 好的,我找到了解决方案。

Got hints here . 在这里有提示。

So the thing is to add a rootScope injection. 因此,要添加一个rootScope注入。 It worked after that! 在那之后它起作用了! :) :)

beforeEach(inject(function($injector) {
     service = $injector.get(
         'service');
     $httpBackend = $injector.get('$httpBackend');
     $rootScope = $injector.get('$rootScope');
 }));

 it('should use the rejection handler if varB is in invalid format',
     function() {
         var successHandler = jasmine.createSpy('success');
         var failHandler = jasmine.createSpy('fail');
         service.func('abc').then(successHandler, failHandler);
         $rootScope.$digest();
         expect(successHandler).not.toHaveBeenCalled();
         expect(failHandler).toHaveBeenCalledWith('varB has to be a number');
     }
 );

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

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