angularjs - 单元测试依赖注入(inject)

标签 angularjs unit-testing jasmine karma-runner

我对 Jasmine 和 karma 是全新的。我相信我的环境设置正确,并且我能够运行非常基本的单元测试,但是一旦我尝试实例化 Controller ,我就会收到未知提供程序错误,并且我不确定如何调试它。我需要传入 stateProvider 依赖项吗?我在 Angular-Seed 示例中没有看到这一点。

鲍尔.json:

{
"name": "starter",
  "description": "A starter project for AngularJS",
  "version": "2.0.0",
  "homepage": "https://starter.com",
  "private": true,
  "dependencies": {
      "angular": "1.2.x",
      "angular-route": "1.2.x",
      "angular-loader": "1.2.x",
      "angular-mocks": "~1.2.15"
  }
}

家庭 Controller :

angular.module('home').controller('Home', function($scope, $rootScope, $state) {

    console.log($scope.pageType);

    $rootScope.pageType = 'home';

    /*
     * Takes in a state and transitions the app to that state.
     */
    $scope.goTo = function(value) {
        $state.transitionTo(value);
    }

    /*
     * Handles what happens after clicking log-in
     */
    $scope.loginClicked = function() {
        $state.transitionTo('log-in');
    }
});

测试文件:

'use strict';

/* jasmine specs for controllers go here */

describe('Home', function() {
    beforeEach(module('home'));

    it('should run tests', inject(function() {
        expect(null).toBeDefined();
    }));

    it('should not say true equals false', function() {
        expect(false).not.toBe(true);
    });

    it('should say true equals true', function() {
        expect(true).toBe(true);
    });

    it('should say false does not equal true', function() {
        expect(false).not.toBe(true);
    });

    it('should create "phones" model with 3 phones', inject(function($controller,$rootScope) {

    /*
     * 
     * COMMENTING OUT THESE LINES = PASS
     *
     */
        var scope = $rootScope.$new(),
            ctrl = $controller('Home', {$scope:scope});
        expect(ctrl).not.toBe(null);
    }));

});

错误:

    Error: [$injector:unpr] Unknown provider: $stateProvider <- $state
http://errors.angularjs.org/1.2.16/$injector/unpr?p0=%24stateProvider%20%3C-%20%24state
    at /Users/jlett/bower_components/angular/angular.js:78:12
    at /Users/jlett/bower_components/angular/angular.js:3705:19
    at Object.getService [as get] (/Users/jlett/bower_components/angular/angular.js:3832:39)
    at /Users/jlett/bower_components/angular/angular.js:3710:45
    at getService (/Users/jlett/bower_components/angular/angular.js:3832:39)
    at invoke (/Users/jlett/bower_components/angular/angular.js:3859:13)
    at Object.instantiate (/Users/jlett/bower_components/angular/angular.js:3880:23)
    at /Users/jlett/bower_components/angular/angular.js:7134:28
    at null.<anonymous> (/Users/jlett/test/unit/home-controller_tests.js:26:20)
    at Object.invoke (/Users/jlett/bower_components/angular/angular.js:3869:17)
Error: Declaration Location
    at window.inject.angular.mock.inject (/Users/jlett/bower_components/angular-mocks/angular-mocks.js:2132:25)
    at null.<anonymous> (/Users/jlett/test/unit/home-controller_tests.js:24:54)
    at /Users/jlett/zoetis-rimadyl-mobile/test/unit/home-controller_tests.js:5:1

最佳答案

如果未包含其中一个可注入(inject)模块,您将收到此错误。

例如,你有

beforeEach(module('home'));

如果您的 $state 依赖项不在 home 模块中,您还需要包含该模块。我不熟悉 $state (我认为它是 angular-ui 的路由器?只有 angular.js 服务应该以 $ 开头)。如果它是 Angular 用户界面,这就是你应该如何设置:

beforeEach(module('ui.router'));
beforeEach(module('home'));

这样,Angular 的测试运行程序就知道运行测试需要哪些模块。

实际上,只要您将 ui.router 依赖项定义为该模块的依赖项,包含 home 模块就应该为您完成此操作。如果配置正确,您可能需要查看测试中包含的文件的顺序。例如,确保测试中包含 ui-router 文件,并且在 karma 配置中的 home 模块之前引用该文件。

关于angularjs - 单元测试依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23253941/

相关文章:

javascript - Angular.js、Yeoman、Grunt

javascript - 如何知道 ngClick 何时在元素上触发?

javascript - AngularJS ng-click 表行但不单击某个 td

c++ - 使用 C++ Boost.Test 组织单元测试?

python - 单元测试 : assert exception-handling function called

c++ - 谷歌在同一解决方案中测试多个 C++ 项目

angular - Uncaught Error : Cannot find module "tslib"

javascript - Angular Directive(指令) : It's possible testing that certain characters are rejected in a keypress event?

javascript - 如何运行单个 Testacular 单元测试?

angularjs - Angular UI 路由器 : abstract : true won't display page