angularjs - How to test vm.variable - function(){..} unit testing with jasmine? -
i using karma- jasmine framework test angular application. i'm facing problem in calling functions of controller or service form test-spec. tried using controller. scope. both of them didn't work me.
the controller code is
(function () {'use strict'; angular.module('selfui').controller('attendeescontroller', attendeescontroller); attendeescontroller.$inject = ['$state', '$stateparams', 'attendeeservice', 'settings', '$log']; function attendeescontroller($state, $stateparams, attendeeservice, settings, $log) { var vm = this; if ($stateparams.attendeedata === null) { vm.pagetype = "add attendee"; vm.isedit = false; } else { var tempattendee = $stateparams.attendeedata; vm.pagetype = "edit attendee"; vm.isedit = true; vm._id = tempattendee._id; vm.firstname = tempattendee.firstname; vm.lastname = tempattendee.lastname; } vm.checkavailable = function(email){ //check email if(email === null || angular.isundefined(email) || email.trim().length === 0){ vm.invalidemail = true; }else{ // check if email present or not success = function(data, status, headers, config) { if(data[0].success){ vm.validemail = true; }else{ if(data[0].message){ modaldata = { success : false, }; }else{ vm.validemail = false; } } }; failure = function(data, status, headers, config) { vm.invalidemail = false; }; attendeeservice.checkemail(email, success, failure); } }; } })();
my test spec is:
describe('attendeescontroller', function(){ beforeeach(module('ss')); var state, $stateparams, stateparams, settings, attendeeservice, log; var email= "temp@email.com"; var controller, $scope; var vm; $stateparams= {_id : "1", attendeetype : "attendee", firstname : "pen", lastname : "red", email : "temp@email.com", company : "swg", jobtitle : "speaker", biography : "bio", tagsinterest : "interests"} beforeeach(inject(function($rootscope, _addattendeeservice_, $controller, $state, $stateparams,settings, $log) { attendeeservice= _attendeeservice_; state= $state; $scope = $rootscope.$new(); controller = function() { return $controller('attendeescontroller', { $scope : scope, $stateparams : $stateparams }); }; })); it('should instantiated', function() { expect(attendeeservice).tobedefined(); }); it('should have controller.methods defined', function(){ expect(controller).tobedefined(); expect(controller.checkavailable).tobeundefined(); controller.checkavailable(email); });
});
my service code looks this:
(function() { 'use strict'; angular.module('ss').service('attendeeservice', attendeeservice); attendeeservice.$inject = ['ssfactory', 'settings', '$rootscope']; function attendeeservice(selfsfactory, settings, $rootscope){ this.addattendee = function(data, success, failure){ var responsepromise = selfservicefactory.httppostrequest(settings.createattendeeurl, data, config); responsepromise.success(success); responsepromise.error(failure); return responsepromise; }; }});
when run test spec, gives me error
typeerror: 'undefined' not function (evaluating 'controller.checkavailable(email)') @ c:/users/ibm_admin/webstormprojects/self/self-service-ui.git/src/tests/addattendees.controller.spec.js:53
i tried calling checkavailable function scope variable too, didn't work. need know how call such methods in controller test spec.
Comments
Post a Comment