Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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
Unit testing 如何测试AngularJS提供程序?_Unit Testing_Angularjs_Jasmine - Fatal编程技术网

Unit testing 如何测试AngularJS提供程序?

Unit testing 如何测试AngularJS提供程序?,unit-testing,angularjs,jasmine,Unit Testing,Angularjs,Jasmine,我需要测试我自己的angular provider,我需要在配置和运行阶段对其进行测试,以检查配置方法是否有效,以及实例化的provider是否确实配置了正确的参数 当我询问提供程序的依赖性注入时,它找不到APIResourceFactoryProvider,只有APIResourceFactory,而且我在迄今为止查找过的存储库中还没有找到任何这样的示例。它实际上比最初在AngularJS中测试提供程序要简单得多: description('测试提供程序',函数()){ 风险值提供者; 每个之

我需要测试我自己的angular provider,我需要在配置和运行阶段对其进行测试,以检查配置方法是否有效,以及实例化的provider是否确实配置了正确的参数


当我询问提供程序的依赖性注入时,它找不到APIResourceFactoryProvider,只有APIResourceFactory,而且我在迄今为止查找过的存储库中还没有找到任何这样的示例。

它实际上比最初在AngularJS中测试提供程序要简单得多:

description('测试提供程序',函数()){
风险值提供者;
每个之前(模块('plunker',函数(myServiceProvider)){
provider=myServiceProvider;
}));
它('在方法调用时应返回true',注入(函数(){
expect(provider.method()).toBeTruthy();
}));
});
```


证据就在Plunker中:

这里有一个小助手,可以正确地封装抓取提供程序,从而确保各个测试之间的隔离:

  /**
   * @description request a provider by name.
   *   IMPORTANT NOTE: 
   *   1) this function must be called before any calls to 'inject',
   *   because it itself calls 'module'.
   *   2) the returned function must be called after any calls to 'module',
   *   because it itself calls 'inject'.
   * @param {string} moduleName
   * @param {string} providerName
   * @returns {function} that returns the requested provider by calling 'inject'
   * usage examples:
    it('fetches a Provider in a "module" step and an "inject" step', 
        function() {
      // 'module' step, no calls to 'inject' before this
      var getProvider = 
        providerGetter('module.containing.provider', 'RequestedProvider');
      // 'inject' step, no calls to 'module' after this
      var requestedProvider = getProvider();
      // done!
      expect(requestedProvider.$get).toBeDefined();
    });
   * 
    it('also fetches a Provider in a single step', function() {
      var requestedProvider = 
        providerGetter('module.containing.provider', 'RequestedProvider')();

      expect(requestedProvider.$get).toBeDefined();
    });
   */
  function providerGetter(moduleName, providerName) {
    var provider;
    module(moduleName, 
           [providerName, function(Provider) { provider = Provider; }]);
    return function() { inject(); return provider; }; // inject calls the above
  }
  • 获取提供者的过程是完全封装的:不需要破坏测试之间隔离的闭包变量
  • 该过程可以分为两个步骤,“模块”步骤和“注入”步骤,这两个步骤可以与单元测试中对“模块”和“注入”的其他调用适当分组
  • 如果不需要拆分,只需一个命令即可检索提供程序
为了防止您的提供商出现小型化版本,事情会变得稍微复杂一些

以下是提供商代码:

angular
    .module('core.services')
    .provider('storageService', [function () {
        function isLocalStorageEnabled(window) {
            return true;
        }

        this.$get = ['$window', 'chromeStorageService', 'html5StorageService',
            function($window, chromeStorageService, html5StorageService) {
            return isLocalStorageEnabled($window) ? html5StorageService : chromeStorageService;
        }];
    }]);
测试用例:

describe('Storage.Provider', function() {
    var chrome = {engine: 'chrome'};
    var html5 = {engine: 'html5'};
    var storageService, provider;

    beforeEach(module('core.services'));
    beforeEach(function () {
        module(function (storageServiceProvider) {
            provider = storageServiceProvider;
        });
    });
    beforeEach(angular.mock.module(function($provide) {
        $provide.value('html5StorageService', html5);
        $provide.value('chromeStorageService', chrome);
    }));

    // the trick is here
    beforeEach(inject(function($injector) {
        storageService = $injector.invoke(provider.$get);
    }));

    it('should return Html5 storage service being run in a usual browser', function () {
        expect(storageService).toBe(html5);
    });
});
在本例中,$get是一个数组,不能将其作为提供依赖项作为参数的普通函数调用。解决方案是使用$injector.invoke()


奇怪的是,大多数教程和示例都忽略了这个细节。

如果不调用
inject
,这是如何工作的?我发现我必须跟随
module();在它之前('test-case'),而不是像它('test-case')这样的每个测试用例都使用它,注入(function(){})