Angularjs unit test app configuration

103 views
Skip to first unread message

Ramesh Paul

unread,
May 27, 2014, 9:24:08 AM5/27/14
to ang...@googlegroups.com

I am developing angularjs app and i have defined app configuration data. When i unit test my controller i am getting error if i remove app configuration unit tests are running with out errors.

My code is here 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":"xx...@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);
});

});

Can any on help me how to add configuration data to the unit test?

André Farzat

unread,
May 27, 2014, 1:37:49 PM5/27/14
to ang...@googlegroups.com
I believe that the right way to add a constant ( or a configuration variable ) is to use `.value` instead of `.config`

Have a look in Configuration Blocks in the doc.

If you do `angular.module('Signup', []).value('ajaxURL', { ... });` you can inject it later as `function( ajaxURL ){ ... }`.
So, in your tests, you can mock the value and inject in the controller.

Ramesh Paul

unread,
May 27, 2014, 3:08:29 PM5/27/14
to ang...@googlegroups.com
Thank you André Farzat now i am able test with config values.


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/onpsdhq3Jm8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
Regards
Ramesh Babu Ch
Reply all
Reply to author
Forward
0 new messages