使用requirejs测试uiRouter

时间:2014-08-12 21:06:14

标签: javascript angularjs requirejs jasmine angular-ui-router

我一直坚持这个问题并且没有成功解决有关使用requirejs测试uiRouter的任何其他问题。我有一个相当基本的控制器设置,一旦点击一个按钮,就会使用$ state.go在状态之间进行转换。

runsController.js

define([],
function() {
"use strict"
var runsController = function($scope, $window, $http, $state) {

    function getRuns() {
        $http.get($window.apiLinks.runs).success(function(data) {
            $scope.runs = data.objects;
        }).error(function(data) {
            console.error();
            console.error(data);
        });
    }
    getRuns();

    $scope.refresh = getRuns;



    $scope.createRun = function() {
        //$state.go('createRun');
    }

    return ["$scope", "$window", "$http", "$state", runsController];
});

控制器被添加到依赖于uiRouter的应用程序。

app.js

define(["angular", "js/controllers/runsController", "js/router", "uiRouter"],
function(angular, runsController, router) {
'use strict'

var appName = "myApp";
var app = angular.module(appName, ["ui.router"]);

app.config(router);

app.controller("runsController", runsController);


function getName() {
    return appName;
}

return {
    app : app,
    getName : getName
};

});

router.js

define(["./controllers/runsController"],
function(runsController){

var routes = function($stateProvider, $urlRouterProvider) {

    // For any unmatched url, redirect to /runs
    $urlRouterProvider.otherwise("/runs");

    // Set up the states
    $stateProvider
        .state('runs', {
            url: "/runs",
            templateUrl: "partials/runs.html",
            controller: "runsController"

        })
        .state('createRun', {
            url: "/createRun",
            templateUrl: "partials/runCreator.html"
        });
};


return ["$stateProvider", "$urlRouterProvider", routes];

});

这是我为此控制器设置的测试:

define(["angular", "angularMocks", "js/app", "js/controllers/runsController"],
function(angular, mocks, app, runsController) {
'use strict'
describe('runsController Unit Tests', function() {
    var mockApp, scope, httpBackend, objects, state;


    objects = [
        {rid : 1, filename : "myFile.txt", exitCode : 0},
        {rid : 2, filename : "test.zip", exitCode : 0},
        {rid : 3, filename : "test2.tar", exitCode : 0}
    ];

    beforeEach(function() {

        mockApp = angular.module(app.getName());
    });

    beforeEach(inject(function ($rootScope, $controller, $httpBackend, $http, $state) {
        scope = $rootScope.$new();
        window.apiLinks = {"runs" : "/mock/api/runs"};
        httpBackend = $httpBackend;
        state = $state;
        httpBackend.when("GET", "/mock/api/runs").respond({
            "objects"  : objects
        });
        $controller(runsController[4], {
            $scope : scope,
            $window : window,
            $http : $http,
            $state : $state
        });
        httpBackend.flush();
    }));

    it("Get state working in test environment", function() {

    });

    it("Test that controller automatically gets the runs", function() {
        // Because of the way our controller files are setup, we have to specify the
        // last element in the array instead of just
        expect(scope.runs.length).toBe(3);
    });


});

目前这给我一条错误消息:错误:[$ injector:unpr]未知提供商:$ stateProvider< - $ state

从我读过的内容来看,我认为这意味着我需要注入控制器运行状态所需的依赖项,但是如果我在每次运行测试之前使用应用程序那么依赖项应该在那里.config()完成了吗?任何人都可以帮我弄清楚它是什么我没有看到这个吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,这就是我想出来的。

这非常重要:

<强> karma.conf.js

 {
    pattern: 'ROOT/TO/YOUR/angular-ui-router/release/angular-ui-router.js',
    included: false
  },

test-main.js :这是你的业力 - 需要 - 配置文件

 paths: {
    // External libraries
    'angular': '/base/PATH/TO/YOUR/angular/angular',
    'angularMocks': '/base/PATH/TO/YOUR/angular-mocks/angular-mocks',
    'uiRouter' : '/base/PATH/TO/YOUR/angular-ui-router/release/angular-ui-router',
.....
  • / base / +您的路径

做SHIMMY

 shim: {
    'angular': { 'exports': 'angular' },
    'angularMocks': { deps: ['angular'], 'exports': 'angular.mock' },
    'uiRouter' : {deps: ['angular'], 'exports' : 'uiRouter'},

现在我们正在进行测试。

定义模块

define([
 'angular',
 'angularMocks',
 'uiRouter',
 .....

现在,当您设置 beforeEach

BeforeEach(inject(function ($injector){
  $state = $injector.get('$state');
  ....

希望这可以帮助你。