mercredi 10 juin 2015

test service call with success () , error () in jasmine

this is one of the functions in my controller

function sendMeetingInvitation(companyId, meetingId) {

            meetingService.sendInvitations(companyId, meetingId)
            .success(function () {
                $state.go('company.meeting.view', {}, { reload: true });
            })
            .error(function () {
                //more code for error handling
            });

    }

below is the test case i am using to test when we calling the sendMeetingInvitation() , whether it should invoke the to the success() block of the service call of meetingService.sendInvitations

describe('EditMeetingCtrl.sendMeetingInvitation()', function () {
    var $rootScope, scope, $controller , $q  , companyService , meetingService ;



    var mockedHttpPromise = {
                            success: function() {}
                          };

    beforeEach(angular.mock.module('MyApp'));


    beforeEach(angular.mock.inject(function (_$httpBackend_, _companyService_ , _meetingService_ ) {
        $httpBackend = _$httpBackend_;
        companyService = _companyService_;
        meetingService = _meetingService_ ;
    }));

        beforeEach(inject(function ($rootScope, $controller , _meetingService_ ) {
            scope = $rootScope.$new();
            createController = function() {
                return $controller('EditMeetingCtrl', {
                $scope: scope,
                meeting : {},
                meetingService : _meetingService_
                }); 
            };
            var controller = new createController();
        }));

         it("should should send invitations", function() {
                spyOn(meetingService, 'sendInvitations').and.returnValue(mockedHttpPromise);
                scope.sendMeetingInvitations(123456, 123456);
                expect(meetingService.sendInvitations).toHaveBeenCalledWith(123456, 123456);
         });


    });

i get this error which is not really helpful .

PhantomJS 1.9.8 (Windows 8) In EditMeetingCtrl EditMeetingCtrl.sendMeetingInvitation() should should send invitations FA
ILED
        TypeError: 'undefined' is not an object (near '...})...')

what am i doing here wrong ? i tried my mockedHttpPromise to following . but same result

var mockedHttpPromise = {
                            success: function() {},
                            error: function(){}
                          };

Aucun commentaire:

Enregistrer un commentaire