在单元测试期间,$ controller未定义为一个控制器

时间:2016-04-30 14:57:09

标签: javascript angularjs unit-testing jasmine

我有2个控制器 - 主要和供应商。 main.controller成功登录我的单元测试,而vendor.controller给我一个未定义的错误,完全相同的单元测试。

单元测试

describe('controllers', () => {
    let vm;

    beforeEach(angular.mock.module('thcmaps-ui'));
    beforeEach(inject(($controller) => {
        vm = $controller('MainController'); // This logs correctly but errors when changed to VendorController
        //vm = $controller('VendorController'); 
    }));

    it('should call stateManagerService when current name is main', () => {
        console.log(vm);
        expect(1).toEqual(1);
    });
});

我不知道这是路由问题还是完全不同......

index.route.js

export function routerConfig($stateProvider, $urlRouterProvider) {
'ngInject';
$stateProvider
    //mobile
    .state('main', {
        url         : '/',
        templateUrl : 'app/main/main.html',
        controller  : 'MainController',
        controllerAs: 'main',
        resolve : {
            location : function(userDataService){
                return userDataService.getLocation()
                    .then(function(data){
                        return data;
                    });
            }
        }
    })
    .state('mobile', {
        url         : '/m/',
        templateUrl : 'app/main/main.html',
        controller  : 'MainController',
        controllerAs: 'main',
        resolve : {
            location : function(userDataService){
                return userDataService.getLocation()
                    .then(function(data){
                        return data;
                    });
            }
        }
    })
    .state('mobile.vendor', {
        url         : 'v/:slug',
        templateUrl : 'app/vendor/vendor.html',
        controller : 'VendorController',
        controllerAs : 'vendor',
        resolve : {
            data : function($stateParams, vendorDataService){
                return vendorDataService.getVendor($stateParams.slug)
                    .then((vendor) =>{
                        return vendor;
                    })
                    .catch(()=>{
                        //todo error handling
                        //console.log(error);
                    })
            },
            location : function(userDataService){
                return userDataService.getLocation()
                    .then(function(data){
                        return data;
                    });
            }
        }
    })
    //desktop
    .state('map.vendor', {
        url         : 'v/:slug',
        templateUrl : 'app/vendor/vendor.html',
        controller : 'VendorController',
        controllerAs : 'vendor',
        resolve : {
            data : function($stateParams, vendorDataService){
                return vendorDataService.getVendor($stateParams.slug)
                    .then((vendor) =>{
                        return vendor;
                    })
                    .catch(()=>{
                        //todo error handling
                        //console.log(error);
                    })
            },
            location : function(userDataService){
                return userDataService.getLocation()
                    .then(function(data){
                        return data;
                    });
            }
        }
    });


  $urlRouterProvider.otherwise('/');
}

index.module.js

import { runBlock } from './index.run';
import { MainController } from './main/main.controller';
import { VendorController } from './vendor/vendor.controller';
import { StateManagerService } from '../app/components/stateManagerService/stateManager.service';


angular.module('thcmaps-ui', ['ngAnimate', 'ngCookies', 'ngTouch', 'ngSanitize', 'ngMessages', 'ui.router',
                              'ngResource', 'ui.bootstrap', 'toastr'])
    .service('stateManagerService', StateManagerService)
    .controller('MainController', MainController)
    .controller('VendorController', VendorController)

0 个答案:

没有答案
相关问题