Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write jasmine test for angular controller and service like this?

I'm stuck for hours for this and it's because I'm new to Jasmine and Angularjs. Basically, I have this controller.

angular.module('toadlane.controllers', []).
    controller('MainController', function($scope, $http, SomeService) {

        var promise = SomeService.getObjects();

        promise.then(function(data) {
            if(data.length > 0) {
                $scope.objs = data;
            } else {
                $scope.message = "Please come back soon";
            }
        });
    });

How can I write a Jasmine test to mock SomeService to stub out data and simulate if there's data the scope's length should not be 0 and if data is [] the message should be "Please come back soon".

'use strict';

describe('controllers', function () {
    var scope, someService;
    beforeEach(module('services'));
    beforeEach(module('controllers'));
    beforeEach(inject(function (SomeService) {
        someService = SomeService;

        var callback = jasmine.createSpy();
        someService.getObjs().then(callback);
    }));

    it('should return some objs', inject(function ($rootScope, $controller) {
        scope = $rootScope.$new();

        expect($controller('MainController', {$scope: scope, SomeService : someService})).toBeTruthy();

        expect(scope.objs.length).toEqual(2);
    }));

    it('should return no objs', inject(function ($rootScope, $controller) {
        scope = $rootScope.$new();

        expect($controller('MainController', {$scope: scope, SomeService : someService})).toBeTruthy();


        expect(scope.message).toEqual("Please come back soon");
    }));

});

The tests are not finished because I don't know how to stub then from promise

Any help is very appreciated. Thanks.

like image 587
toy Avatar asked Dec 03 '25 10:12

toy


1 Answers

Defers can be tested by mocking promises with functions which return object with chained methods. In this case mocked SomeService.getObjs should return an object with method then.

In order to do this add another one beforeEach statement:

beforeEach(module(function ($provide) {
  $provide.value('SomeService', {
    getObjs: function () {
      return {
        then: function (callback) {
          // mockedResponseData is passed to callback
          callback(mockedResponseData);
        }
      };
    }
  }));

Mocked service will immediately load result in existing promise callback function.

like image 91
Dmitry Evseev Avatar answered Dec 04 '25 22:12

Dmitry Evseev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!