Angularjs单元测试应用程序配置

时间:2014-05-27 13:19:58

标签: angularjs unit-testing jasmine karma-jasmine

我正在开发angularjs应用程序,我已经定义了应用程序配置数据。当我对我的控制器进行单元测试时,如果我删除应用程序配置单元测试运行时没有错误,我会收到错误。

我的代码在这里 app.js

var signup = angular.module('Signup', []);
    signup.config("ajaxURL",{"signupSubmit": "/signup/submit","signupCheckEmailAvailability": "/signup/checkemail"});

    signup.factory('SignupService',['$http','ajaxURL',function($http,URL){
        return {
          submit: function(signupData){
                console.log("in submit service--");
                console.log(signupData);
                var promise = $http.post((URL.signupSubmit).toString(), signupData).then(function(response){
                    return response;
                });
                return promise;
          },
          checkEmailAvailability: function(emailData){
              var promise = $http.post((URL.signupCheckEmailAvailability).toString(),emailData).then(function(response){
                  return response;
              });
              return promise;
          }
        };
    }]);

    signup.controller('SignupCtrl',['SignupService', '$scope',function(Signup, $scope){

        $scope.signupPromise;            
        $scope.submitSignupForm = function(signupData){
            $scope.signupPromise = Signup.submit(signupData);
            signupSubmitEvent();
        }

        function signupSubmitEvent(){
            $scope.signupPromise.then(function(response){
                console.log("http response");
            });
        }

    }]);

appSpec.js

'use strict';
describe('signup unit tests', function() {

var signup, scope, $httpBackend, ctrl;
var userData = {"userid":"2","email":"xxxx@sss.com","clientId":"123456789","clientSecret":"a1b2c3d4e5f6","accessToken":"AP16MD3217"};

beforeEach(module('Signup'));

beforeEach(inject(function($injector, _$rootScope_, $controller, _SignupService_){

    $httpBackend = $injector.get('$httpBackend');

    scope = _$rootScope_.$new();
    var Signup = _SignupService_;

    ctrl = $controller('SignupCtrl', {Signup: Signup, $scope: scope});
}));

it("simple testing", function(){
    console.log("in simple test");
    $httpBackend.expectPOST("/signup/submit/", userData).respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);
    $httpBackend.flush();
    scope.submitSignupForm(userData);
});

});

可以帮助我如何将配置数据添加到单元测试中吗?

1 个答案:

答案 0 :(得分:0)

如果要在不运行应用程序的配置部分的情况下测试服务/控制器,则需要将控制器和服务放入单独的模块中。 e.g:

var signup = angular.module('Signup', ['Signup.services', 'Signup.ctrls']);

signup.config("ajaxURL",{"signupSubmit": "/signup/submit","signupCheckEmailAvailability": "/signup/checkemail"});

var signupServices = angular.module('Signup.services', []);
signupServices.factory('SignupService',['$http','ajaxURL',function($http,URL){
    return {
      ...
    };
}]);

var signupControllers = angular.module('Signup.ctrls', ['Signup.services']);
signupControllers.controller('SignupCtrl',['SignupService', '$scope',function(Signup, $scope){
  ...
}]);

然后在测试中,您可以加载要测试的模块。 e.g:

beforeEach(module('Signup.services'));