【问题标题】:Unknown provider CookieStore未知提供者 CookieStore
【发布时间】:2013-09-27 11:41:53
【问题描述】:

我正在使用 $cookieStore 模块的服务。它工作正常,但是在单元测试时它会中断,并给出错误:“$cookieStoreProvider

服务如下所示:

serviceModule.factory('filtersService', ['$rootScope', '$location', '$cookieStore', function($rootScope, $location, $cookieStore){
    return {
        getFilters: function(){...}
}

单元测试服务如下所示:

describe('filtersService tests', function(){
var filtersService;
beforeEach(module('App.services'));

beforeEach(inject(function(filtersService, urlService, $location){

    filtersService = filtersService;
    urlService = urlService;
}));

it('test something', inject(function(filtersService, $location){
    filtersService.getFilters();
   expect(...something...)
}));
});

我在 karma 测试文件中包含了 angular-cookies。

知道为什么代码在运行时有效,但单元测试失败了吗?

更新:

Karma Config File:
// Karma configuration
// Generated on Sun May 12 2013 16:57:21 GMT+0200 (CEST)


// base path, that will be used to resolve files and exclude
basePath = '../';


// list of files / patterns to load in the browser
files = [
  JASMINE,
  JASMINE_ADAPTER,
  'test/karma/jquery-1.9.1.min.js',
  'app/assets/javascripts/sugar-1.3.9.min.js',
  'app/assets/javascripts/angular.js',
  'app/assets/javascripts/angular-*.js',
  'app/assets/javascripts/ui-bootstrap-tpls-0.4.0.min.js',
  'app/assets/javascripts/services.js',
  'test/karma/lib/angular/angular-mocks.js',
  'test/karma/unit/*/*.js'
];


// list of files to exclude
exclude = [

];


// test results reporter to use
// possible values: 'dots', 'progress', 'junit'
reporters = ['progress'];


// web server port
port = 9876;


// cli runner port
runnerPort = 9100;


// enable / disable colors in the output (reporters and logs)
colors = true;


// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel = LOG_INFO;


// enable / disable watching file and executing tests whenever any file changes
autoWatch = true;


// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers = ['Chrome'];


// If browser does not capture in given timeout [ms], kill it
captureTimeout = 60000;


// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun = false;

【问题讨论】:

  • 你能显示业力配置文件吗?
  • @Blackhole 当然是现在添加了。
  • 您不必在files 数组中包含cookies.js 文件吗?就像你在 index.html 中所做的那样:<script src="http://code.angularjs.org/1.0.0rc10/angular-cookies-1.0.0rc10.js"></script>
  • @EduardGamonal 通过使用 'app/assets/javascripts/angular-*.js',我还包括了 angular-cookies.min.js。
  • 你在哪里使用cookieStore?也许它也应该注入到beforeEach 块中

标签: angularjs jasmine angularjs-service karma-runner


【解决方案1】:

关键是 angular.js 版本和 angular-cookies 版本必须相同。

在我匹配它们之后,它对我有用。

【讨论】:

    【解决方案2】:

    我在使用 angular-cookies 和 angular-file-upload 时遇到了类似的问题。我是这样解决的:

    首先,我使用 bower list 检查了我的应用程序的所有依赖项:

    -bash-4.1$ bower list
    bower check-new     Checking for new versions of the project dependencies..
     APPNAME#0.0.0 /home/git/APPNAME/test.git
     ├── angular#1.2.28 (1.4.4-build.4150+sha.39ff333 available)
     ├─┬ angular-bootstrap#0.11.2 (latest is 0.13.2)
     │ └── angular#1.2.28 (1.4.4-build.4150+sha.39ff333 available)
     ├─┬ angular-cookies#1.4.3 (1.4.4-build.4150+sha.39ff333 available)
     │ └── angular#1.2.28 incompatible with 1.4.3 (1.4.3 available, latest is 1.4.4-build.4150+sha.39ff333)
     ├─┬ angular-file-upload#1.2.0
     │ └── angular#1.2.28 (1.2.29-build.592+sha.b041b66 available, latest is 1.4.4-build.4150+sha.39ff333)
    

    正如您将看到的,bower 告诉我 Angular 1.2.28 和 1.4.3 之间的不兼容,然后,我们遇到了库问题。 angular-cookies 和 angular-file-upload 也是如此。

    在那之后,我决定用rm -fr bower_components 清理我所有的 bower_components 目录并运行一个新的bower install 以确保所有库都是“全新安装的”。

    -bash-4.1$ rm -fr bower_components
    -bash-4.1$ cd ..
    -bash-4.1$ cd ..
    -bash-4.1$ bower install
    bower jquery#~1.11.0            cached git://github.com/jquery/jquery.git#1.11.3
    bower jquery#~1.11.0          validate 1.11.3 against git://github.com/jquery/jquery.git#~1.11.0
    bower es5-shim#~3.0.1           cached git://github.com/es-shims/es5-shim.git#3.0.2
    bower es5-shim#~3.0.1         validate 3.0.2 against git://github.com/es-shims/es5-shim.git#~3.0.1
    bower angular-resource#>=1.2.*  cached git://github.com/angular/bower-angular-resource.git#1.4.3
    bower angular-resource#>=1.2.*         validate 1.4.3 against git://github.com/angular/bower-angular-resource.git#>=1.2.*
    bower angular-cookies#>=1.2.*            cached git://github.com/angular/bower-angular-cookies.git#1.4.3
    bower angular-cookies#>=1.2.*          validate 1.4.3 against git://github.com/angular/bower-angular-cookies.git#>=1.2.*
    bower angular-sanitize#>=1.2.*           cached git://github.com/angular/bower-angular-sanitize.git#1.4.3
    bower angular-sanitize#>=1.2.*         validate 1.4.3 against git://github.com/angular/bower-angular-sanitize.git#>=1.2.*
    bower bootstrap#~3.1.1                   cached git://github.com/twbs/bootstrap.git#3.1.1
    bower bootstrap#~3.1.1                 validate 3.1.1 against git://github.com/twbs/bootstrap.git#~3.1.1
    bower bootstrap-sass-official#~3.1.1     cached git://github.com/twbs/bootstrap-sass.git#3.1.1+2
    bower bootstrap-sass-official#~3.1.1   validate 3.1.1+2 against git://github.com/twbs/bootstrap-sass.git#~3.1.1
    bower lodash#~3.9.3                      cached git://github.com/lodash/lodash.git#3.9.3
    bower lodash#~3.9.3                    validate 3.9.3 against git://github.com/lodash/lodash.git#~3.9.3
    bower angular-socket-io#~0.6.0           cached git://github.com/btford/angular-socket-io.git#0.6.1
    bower angular-socket-io#~0.6.0         validate 0.6.1 against git://github.com/btford/angular-socket-io.git#~0.6.0
    bower angular-ui-router#~0.2.10          cached git://github.com/angular-ui/ui-router.git#0.2.15
    bower angular-ui-router#~0.2.10        validate 0.2.15 against git://github.com/angular-ui/ui-router.git#~0.2.10
    bower angular-bootstrap#~0.11.0          cached git://github.com/angular-ui/bootstrap-bower.git#0.11.2
    bower angular-bootstrap#~0.11.0        validate 0.11.2 against git://github.com/angular-ui/bootstrap-bower.git#~0.11.0
    bower textAngular#~1.4.1                 cached git://github.com/fraywing/textAngular.git#1.4.3
    bower textAngular#~1.4.1               validate 1.4.3 against git://github.com/fraywing/textAngular.git#~1.4.1
    bower angular-file-upload#~1.2.0         cached git://github.com/nervgh/angular-file-upload.git#1.2.0
    bower angular-file-upload#~1.2.0       validate 1.2.0 against git://github.com/nervgh/angular-file-upload.git#~1.2.0
    bower angular-mocks#>=1.2.*              cached git://github.com/angular/bower-angular-mocks.git#1.4.3
    bower angular-mocks#>=1.2.*            validate 1.4.3 against git://github.com/angular/bower-angular-mocks.git#>=1.2.*
    bower angular-google-places-autocomplete#~0.2.7           cached git://github.com/kuhnza/angular-google-places-autocomplete.git#0.2.7
    bower angular-google-places-autocomplete#~0.2.7         validate 0.2.7 against git://github.com/kuhnza/angular-google-places-autocomplete.git#~0.2.7
    bower angular-scenario#>=1.2.*                            cached git://github.com/angular/bower-angular-scenario.git#1.4.3
    bower angular-scenario#>=1.2.*                          validate 1.4.3 against git://github.com/angular/bower-angular-scenario.git#>=1.2.*
    bower json3#~3.3.1                                        cached git://github.com/bestiejs/json3.git#3.3.2
    bower json3#~3.3.1                                      validate 3.3.2 against git://github.com/bestiejs/json3.git#~3.3.1
    bower font-awesome#>=4.1.0                                cached git://github.com/FortAwesome/Font-Awesome.git#4.4.0
    bower font-awesome#>=4.1.0                              validate 4.4.0 against git://github.com/FortAwesome/Font-Awesome.git#>=4.1.0
    bower angular-google-maps#~2.1.5                          cached git://github.com/angular-ui/angular-google-maps.git#2.1.5
    bower angular-google-maps#~2.1.5                        validate 2.1.5 against git://github.com/angular-ui/angular-google-maps.git#~2.1.5
    bower angular#1.4.3                                       cached git://github.com/angular/bower-angular.git#1.4.3
    bower angular#1.4.3                                     validate 1.4.3 against git://github.com/angular/bower-angular.git#1.4.3
    bower font-awesome#>=4.0.x                                cached git://github.com/FortAwesome/Font-Awesome.git#4.4.0
    bower font-awesome#>=4.0.x                              validate 4.4.0 against git://github.com/FortAwesome/Font-Awesome.git#>=4.0.x
    bower rangy#~1.3.0                                        cached git://github.com/timdown/rangy-release.git#1.3.0
    bower rangy#~1.3.0                                      validate 1.3.0 against git://github.com/timdown/rangy-release.git#~1.3.0
    bower angular#~1.2.11                                     cached git://github.com/angular/bower-angular.git#1.2.28
    bower angular#~1.2.11                                   validate 1.2.28 against git://github.com/angular/bower-angular.git#~1.2.11
    bower angular                                         resolution Unsuitable resolution declared for angular: 1.4.1
    
    Unable to find a suitable version for angular, please choose one:
        1) angular#~1.2.11 which resolved to 1.2.28 and is required by angular-file-upload#1.2.0
        2) angular#>=1 which resolved to 1.2.28 and is required by angular-bootstrap#0.11.2
        3) angular#>= 1.0.8 which resolved to 1.2.28 and is required by angular-ui-router#0.2.15
        4) angular#^1.2.6 which resolved to 1.2.28 and is required by angular-socket-io#0.6.1
        5) angular#^1.2.x which resolved to 1.2.28 and is required by angular-google-places-autocomplete#0.2.7
        6) angular#1.2 - 1.4 which resolved to 1.2.28 and is required by angular-google-maps#2.1.5
        7) angular#1.4.3 which resolved to 1.4.3 and is required by angular-cookies#1.4.3, angular-mocks#1.4.3, angular-resource#1.4.3, angular-sanitize#1.4.3, angular-scenario#1.4.3
        8) angular#>=1.3.x which resolved to 1.4.3 and is required by textAngular#1.4.3
    
    Prefix the choice with ! to persist it to bower.json
    
    ? Answer: 7!
    

    bower 让我在 angular 和 I´ve 版本之间进行选择 选择了第 7 个选项,因为它是 angular 1.4.3,最新的 我的应用程序的大多数组件所需的版本。 之后,bower 进行了安装,并将 angular 1.4.3 链接到所有需要它的 bower_components:

    bower angular                                         resolution Saved angular#1.4.3 as resolution
    bower es5-shim#~3.0.1                                    install es5-shim#3.0.2
    bower angular-sanitize#>=1.2.*                           install angular-sanitize#1.4.3
    bower bootstrap-sass-official#~3.1.1                     install bootstrap-sass-official#3.1.1+2
    bower jquery#~1.11.0                                     install jquery#1.11.3
    bower angular-resource#>=1.2.*                           install angular-resource#1.4.3
    bower angular-cookies#>=1.2.*                            install angular-cookies#1.4.3
    bower angular-bootstrap#~0.11.0                          install angular-bootstrap#0.11.2
    bower angular-ui-router#~0.2.10                          install angular-ui-router#0.2.15
    bower lodash#~3.9.3                                      install lodash#3.9.3
    bower angular-socket-io#~0.6.0                           install angular-socket-io#0.6.1
    bower bootstrap#~3.1.1                                   install bootstrap#3.1.1
    bower angular-google-places-autocomplete#~0.2.7          install angular-google-places-autocomplete#0.2.7
    bower textAngular#~1.4.1                                 install textAngular#1.4.3
    bower angular-file-upload#~1.2.0                         install angular-file-upload#1.2.0
    bower json3#~3.3.1                                       install json3#3.3.2
    bower angular-mocks#>=1.2.*                              install angular-mocks#1.4.3
    bower rangy#~1.3.0                                       install rangy#1.3.0
    bower angular-google-maps#~2.1.5                         install angular-google-maps#2.1.5
    bower font-awesome#>=4.0.x                               install font-awesome#4.4.0
    bower angular#1.4.3                                      install angular#1.4.3
    bower angular-scenario#>=1.2.*                           install angular-scenario#1.4.3
    
    es5-shim#3.0.2 client/bower_components/es5-shim
    
    angular-sanitize#1.4.3 client/bower_components/angular-sanitize
    └── angular#1.4.3
    
    bootstrap-sass-official#3.1.1+2 client/bower_components/bootstrap-sass-official
    
    jquery#1.11.3 client/bower_components/jquery
    
    angular-resource#1.4.3 client/bower_components/angular-resource
    └── angular#1.4.3
    
    angular-cookies#1.4.3 client/bower_components/angular-cookies
    └── angular#1.4.3
    
    angular-bootstrap#0.11.2 client/bower_components/angular-bootstrap
    └── angular#1.4.3
    
    angular-ui-router#0.2.15 client/bower_components/angular-ui-router
    └── angular#1.4.3
    
    lodash#3.9.3 client/bower_components/lodash
    
    angular-socket-io#0.6.1 client/bower_components/angular-socket-io
    └── angular#1.4.3
    
    bootstrap#3.1.1 client/bower_components/bootstrap
    └── jquery#1.11.3
    
    angular-google-places-autocomplete#0.2.7 client/bower_components/angular-google-places-autocomplete
    └── angular#1.4.3
    
    textAngular#1.4.3 client/bower_components/textAngular
    ├── angular#1.4.3
    ├── font-awesome#4.4.0
    └── rangy#1.3.0
    
    angular-file-upload#1.2.0 client/bower_components/angular-file-upload
    └── angular#1.4.3
    
    json3#3.3.2 client/bower_components/json3
    
    angular-mocks#1.4.3 client/bower_components/angular-mocks
    └── angular#1.4.3
    
    rangy#1.3.0 client/bower_components/rangy
    
    angular-google-maps#2.1.5 client/bower_components/angular-google-maps
    ├── angular#1.4.3
    └── lodash#3.9.3
    
    font-awesome#4.4.0 client/bower_components/font-awesome
    
    angular#1.4.3 client/bower_components/angular
    
    angular-scenario#1.4.3 client/bower_components/angular-scenario
    └── angular#1.4.3
    

    现在,我再次运行bower list,检查依赖关系 现在是:

    -bash-4.1$ bower list
    bower check-new     Checking for new versions of the project dependencies..
    appName#0.0.0 /home/git/appName/test.git
    ├─┬ angular-bootstrap#0.11.2 (latest is 0.13.2)
    │ └── angular#1.4.3 (1.4.4-build.4150+sha.39ff333 available)
    ├─┬ angular-cookies#1.4.3 (1.4.4-build.4150+sha.39ff333 available)
    │ └── angular#1.4.3 (latest is 1.4.4-build.4150+sha.39ff333)
    ├─┬ angular-file-upload#1.2.0
    │ └── angular#1.4.3 incompatible with ~1.2.11 (1.2.29-build.592+sha.b041b66 available, latest is 1.4.4-build.4150+sha.39ff333)
    ├─┬ angular-google-maps#2.1.5
    │ ├── angular#1.4.3 (1.4.4-build.4150+sha.39ff333 available)
    │ └── lodash#3.9.3 (3.10.1 available)
    ├─┬ angular-google-places-autocomplete#0.2.7
    │ └── angular#1.4.3 (1.4.4-build.4150+sha.39ff333 available)
    ├─┬ angular-mocks#1.4.3 (1.4.4-build.4150+sha.39ff333 available)
    │ └── angular#1.4.3
    ├─┬ angular-resource#1.4.3 (1.4.4-build.4150+sha.39ff333 available)
    │ └── angular#1.4.3
    ├─┬ angular-sanitize#1.4.3 (1.4.4-build.4150+sha.39ff333 available)
    │ └── angular#1.4.3
    ├─┬ angular-scenario#1.4.3 (1.4.4-build.4150+sha.39ff333 available)
    │ └── angular#1.4.3
    ├─┬ angular-socket-io#0.6.1 (latest is 0.7.0)
    │ └── angular#1.4.3 (1.4.4-build.4150+sha.39ff333 available)
    ├─┬ angular-ui-router#0.2.15
    │ └── angular#1.4.3 (1.4.4-build.4150+sha.39ff333 available)
    ├─┬ bootstrap#3.1.1 (latest is 3.3.5)
    │ └── jquery#1.11.3 (3.0.0-alpha1+compat available)
    ├── bootstrap-sass-official#3.1.1+2 (latest is 3.3.5)
    ├── es5-shim#3.0.2 (latest is 4.1.10)
    ├── font-awesome#4.4.0
    ├── jquery#1.11.3 (latest is 3.0.0-alpha1+compat)
    ├── json3#3.3.2
    ├── lodash#3.9.3 (latest is 3.10.1)
    └─┬ textAngular#1.4.3
      ├── angular#1.4.3 (1.4.4-build.4150+sha.39ff333 available)
      ├── font-awesome#4.4.0
      └── rangy#1.3.0
    

    正如您将看到的,我仍然在 angular-file-upload 组件中看到不兼容的依赖项。然后,我遇到了组件目录,然后 为此组件在本地安装了新版本的 angular( 组件本身需要一个):

    -bash-4.1$ cd client/
    -bash-4.1$ cd bower_components/
    -bash-4.1$ cd angular-file-upload/
    -bash-4.1$ bower install angular --save
    bower angular#*                 cached git://github.com/angular/bower-angular.git#1.4.3
    bower angular#*               validate 1.4.3 against git://github.com/angular/bower-angular.git#*
    
    Unable to find a suitable version for angular, please choose one:
        1) angular#~1.2.11 which resolved to 1.2.28 and is required by angular-file-upload
        2) angular#~1.4.3 which resolved to 1.4.3
    
    Prefix the choice with ! to persist it to bower.json
    
    ? Answer: 1!
    bower angular               resolution Saved angular#~1.2.11 as resolution
    

    之后,一切正常,没有更多的注射或 依赖问题。希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      OP 使用工厂创建了拦截器。但是,我正在修复一些代码,并看到拦截器被创建为提供者......

      .provider('myHttpInterceptor', function 
          HttpInterceptorProvider($injector, _, apiConfig) {
                      var self = this;
      ...
      

      如果您遇到这种情况并偶然发现这个 SO 问题,这就是我解决问题的方法。

      我尝试在函数中插入 $cookieStore,甚至尝试使用 $inject,但我不断收到 Unknown provider CookieStore(因此我找到了这篇文章)

      我解决问题的方法是找到 $get 方法并将其放入 cookie 存储中。

      this.$get = function($q, $cookieStore, myCache) {
                      return {
                          'request': function(config) {
      ...
      

      回想起来,这与provider recipe documentation相当一致。

      【讨论】:

        【解决方案4】:

        从上面的 cmets 看来,对 'ngCookies' 的依赖似乎是在一个单独的模块中声明的。

        要解决此问题,请尝试在您的服务模块中声明它:

        var serviceModule = angular.app('App.services', ['ngCookies']);
        

        这是解决此问题的推荐方法。

        如果由于某种原因您无法更改 serviceModule 定义,您也可以在单元测试中再次声明它:

        beforeEach(module('ngCookies','App.services'));
        

        但是我建议您在代码库中更改它,这样您的 serviceModule 就不会依赖于您的主 App 模块才能正常运行。

        【讨论】:

        • 我要补充一点,这个解决方案还需要包含 angular-cookies.js。
        猜你喜欢
        • 1970-01-01
        • 2016-11-29
        • 2018-09-28
        • 2014-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多