需要/ Angular / Karma / ui-router - 错误:无法解决'登陆'来自州''

时间:2016-03-07 22:48:47

标签: javascript angularjs unit-testing karma-jasmine

我已经看到这个症状发布在其他问题上,而且我最接近我的问题是这个离子问题:

Could not resolve '...' from state ''

PhantomJS 1.9.8(Mac OS X 0.0.0)控制器:ContainerCtrl测试FAILED     错误:无法解决'登陆'来自州'

我是Angular的Karma测试的新手,并且很难调试范围问题。

app.js

.config(function($stateProvider, $urlRouterProvider) {
  // For any unmatched url, redirect to landing
  $urlRouterProvider.otherwise("/");

  // Now set up the states
  $stateProvider
    .state('guide', {
      url: "/guide",
      templateUrl: "views/guide.html",
      controller: 'SurveyController as main'
    })
    .state('stop', {
      url: "/stop",
      templateUrl: "views/stop.html",
      controller: 'StopCtrl as stop'
    })
    .state('landing', {
      url: "/",
      templateUrl: "views/landing.html",
      controller: 'AboutCtrl as about'
    });
});

containerSpec.js

define(['angular', 'angular-mocks', 'app'], function(angular, mocks, app) {
'use strict';

 describe('Controller: ContainerCtrl', function () {

  var ContainerCtrl, $state;

  beforeEach(angular.mock.module('app.controllers.ContainerCtrl', 'ui.router'));


    beforeEach(angular.mock.inject(function($rootScope, $controller, _$state_) {
     //create an empty scope
     ContainerCtrl = $rootScope.$new();
     $state = _$state_;
     //declare the controller and inject our empty scope
     $controller('ContainerCtrl', { $scope : ContainerCtrl });

    }));

  });
});

container.js

define([
    'angular',
    'services/dataservice'], function (
        angular,
        dataService // jshint ignore:line
        ) {
'use strict';

angular.module('oslerApp.controllers.ContainerCtrl', [])
    .controller('ContainerCtrl', [
            '$scope',
            '$rootScope',
            '$state',
                function(
                    $scope,
                    $rootScope,
                    $state) {

            $state.go('landing');

    }]);

});

更新: 更改为logLevel:config.LOG_DEBUG,我看到Spec文件的加载顺序是在app.js和ui.router文件之前。

INFO [PhantomJS 1.9.8 (Mac OS X 0.0.0)]: Connected on socket B-PFcQHXMLMqx8cOoznu with id 19321969
DEBUG [launcher]: PhantomJS (id 19321969) captured in 4.068 secs
DEBUG [web-server]: serving: /deploy/node_modules/karma/static/context.html
DEBUG [web-server]: serving (cached): /deploy/node_modules/requirejs/require.js
DEBUG [web-server]: serving (cached): /deploy/node_modules/karma-requirejs/lib/adapter.js
DEBUG [web-server]: serving (cached): /deploy/test/test-main.js
DEBUG [web-server]: serving (cached): /deploy/bower_components/jquery/dist/jquery.js
DEBUG [web-server]: serving (cached): /deploy/node_modules/karma-jasmine/lib/jasmine.js
DEBUG [web-server]: serving (cached): /deploy/node_modules/karma-jasmine/lib/boot.js
DEBUG [web-server]: serving (cached): /deploy/node_modules/karma-jasmine/lib/adapter.js
DEBUG [web-server]: serving (cached): /deploy/bower_components/angular/angular.js
DEBUG [web-server]: serving (cached): /deploy/test/spec/controllers/containerSpec.js
DEBUG [web-server]: serving (cached): /deploy/test/spec/services/dataserviceSpec.js
DEBUG [web-server]: serving (cached): /deploy/app/scripts/app.js
DEBUG [web-server]: serving (cached): /deploy/bower_components/angular-ui-router/release/angular-ui-router.js
DEBUG [web-server]: serving (cached): /deploy/app/scripts/services/dataservice.js
DEBUG [web-server]: serving (cached): /deploy/app/scripts/controllers/container.js

1 个答案:

答案 0 :(得分:1)

您正尝试通过$state.go命令转到登陆状态,但尚未在app.js中定义状态登陆。解决这个问题并了解你的进展情况。