如何使用requirejs在AngularJS中编写单元测试?

时间:2015-04-04 16:14:00

标签: javascript angularjs requirejs

我在angularjs应用程序中使用requirejs。编写单元测试时我遇到了错误。

这是我的模块定义

define(['angular', 'dropdown', 'authorizationController',
        'cropImageDirective', 'uniqueNameDirective', 'compareDirective',
        'accountNameService', 'uniqueEmailService',
        'angularRoute', 'angularAnimate', 'angularMaterial',
        'angularAria', 'angularMessages', 'uiBootstrap', 'imageCrop'],
    function (angular, dropdown, authorizationController,
              cropImageDirective, uniqueNameDirective, compareDirective,
              accountNameService, uniqueEmailService,
              angularRoute, angularAnimate, angularMaterial,
              angularAria, angularMessages, uiBootstrap, imageCrop) {

        // Declare app level module which depends on filters, and services
        var app = angular.module("ctxadmauth", ['authorization.controllers',
            'cropImage.directives', 'uniqueNames.directives', 'compare.directive',
            'accountName.services', 'uniqueEmail.services',
            'ngRoute', 'ngAnimate', 'ngMaterial', 'ngAria', 'ngMessages', 'ui.bootstrap', 'ngImgCrop']);

        return app;
    });

这是我的控制器规范 -

describe('test suite for authorization module authorization controller', function () {

    var $scope, $rootScope, ctrl, $httpBackend, controller, $location, $window, $interval, $mdDialog;

    //beforeEach(angular.module('ctxadmauth'));
    beforeEach(module('authorization.controllers'));

    beforeEach(
        inject(function (_$rootScope_, _$controller_, _$httpBackend_, _$location_, _$window_, _$interval_, _$mdDialog_) {
            // create a scope object for us to use.
            $scope = _$rootScope_.$new();
            $httpBackend = _$httpBackend_;
            controller = _$controller_;
            $location = _$location_;
            $window = _$window_;
            $interval = _$interval_;
            $mdDialog = _$mdDialog_;

            ctrl = controller('authorizationController', {
                $scope: $scope,
                $rootScope: $rootScope

            });
        })
    );

    describe('controller methods', function () {

        it('should authorization controller to be defined ', function () {
            //spec body
            expect(ctrl).toBeDefined();
        });

        it('should authorization controller calls login account page ', function () {

            expect($scope.loginAccount()).toBeDefined();
            $scope.loginAccount();
            expect($location.path()).toEqual('/admin/login/account');
        });

        it('should authorization controller calls forgot password page ', function () {
            $scope.forgotPassword();
            expect($location.path()).toMatch('/admin/forgotpassword');

        });
    });
});
以前我也以同样的方式做过,但我没有使用requirejs。那时候我成功了。现在业力没有识别模块......在之前(模块(ctxadmauth))。它说是注射错误

请帮我一个正在做angularjs + requirejs组合以及如何在这个组合中编写单元测试的人

0 个答案:

没有答案