mock angularjs service for unit testing using jasmine

r.bhardwaj

I have an Angular App and I want to create unit test cases for it using jasmine.
In my AngularJS app, I have a service as :

var canceler;
var myServices = angular.module('myServices', ['ngResource'])

myServices.factory('getData', ['$http', '$q', function($http, $q){
    var canceler;
    return {
        setData: function(url, callback, error) {
                    canceler = $q.defer();
                    $http.post(url, {}, {timeout:canceler.promise}).success(callback).error(error);
        },
        abort: function(){ canceler.resolve();}
    }
}]);

This service is being used by controller.

Now how can I provide a mock for this "getData" service to the injector I am using in controllerSpecs.js (for the unit testing using jasmine).

For the reference, the code of controllerSpecs.js is defined in Getting error while using Jasmine with AngularJS.

IgorCh

Try something like this

//creation mock service
var mockGetData = {
  data: [],
  setData: function(url, callback, error){
    //emulate real work, any logic can be here
    data.push('called');
    callback(data);
  }
}

and use of the mock service

it('test description', inject(function ($rootScope, $controller){
  var scope = $rootScope.$new();
  var ctrl = $controller('TodoController', { $scope: scope, getData: mockGetData });
  //some test here....
}))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unit Testing an Angular Service using Karma and Jasmine

From Dev

Angularjs Unit Test Not Hitting then part of controller after calling service in AngularJS & testing using Karma & Jasmine

From Dev

Angularjs Unit Test Not Hitting then part of controller after calling service in AngularJS & testing using Karma & Jasmine

From Dev

Unit testing Angularjs jasmine

From Dev

AngularJS Unit Testing Controller w/ service dependency in Jasmine

From Dev

AngularJS testing : Jasmine mock callback

From Dev

expect() in .then() - jasmine unit testing, AngularJS

From Dev

Unit Testing AngularJS Service

From Dev

Unit testing a service in angularJS

From Dev

AngularJS - unit tests - using original service from mock of same service?

From Dev

jasmine testing a mock service in an angular 1.5 controller

From Dev

angularjs unit test karma with jasmine mock dilemma

From Dev

How to Mock Service Initialization Parameters in Service Fabric for Unit Testing similar to mocking the state using a mock class?

From Java

How do I mock a service that returns promise in AngularJS Jasmine unit test?

From Dev

Unit testing an angular service method in jasmine

From Dev

Unit-Testing a filter with Jasmine in AngularJS

From Dev

Unit-testing slider directive with AngularJS + Jasmine

From Dev

Unit-Testing a filter with Jasmine in AngularJS

From Dev

Unit testing of Services / Factories - AngularJS - Jasmine

From Dev

ReferenceError : AngularJS is not defined on Karma with Jasmine unit testing

From Dev

Unit Testing AngularJS and PouchDB Service

From Dev

Unit testing the AngularJS $window service

From Dev

Angularjs unit testing service with promise

From Dev

Unit Testing AngularJS and PouchDB Service

From Dev

AngularJS Controller As Jasmine test with mock service with promise

From Dev

AngularJS testing with Jasmine unable to call "angular.mock.module" while using angular-mocks.js

From Dev

Unit test service that returns promise Angularjs Jasmine

From Dev

Unit test service that returns promise Angularjs Jasmine

From Dev

how to mock $state.params in jasmine unit testing

Related Related

  1. 1

    Unit Testing an Angular Service using Karma and Jasmine

  2. 2

    Angularjs Unit Test Not Hitting then part of controller after calling service in AngularJS & testing using Karma & Jasmine

  3. 3

    Angularjs Unit Test Not Hitting then part of controller after calling service in AngularJS & testing using Karma & Jasmine

  4. 4

    Unit testing Angularjs jasmine

  5. 5

    AngularJS Unit Testing Controller w/ service dependency in Jasmine

  6. 6

    AngularJS testing : Jasmine mock callback

  7. 7

    expect() in .then() - jasmine unit testing, AngularJS

  8. 8

    Unit Testing AngularJS Service

  9. 9

    Unit testing a service in angularJS

  10. 10

    AngularJS - unit tests - using original service from mock of same service?

  11. 11

    jasmine testing a mock service in an angular 1.5 controller

  12. 12

    angularjs unit test karma with jasmine mock dilemma

  13. 13

    How to Mock Service Initialization Parameters in Service Fabric for Unit Testing similar to mocking the state using a mock class?

  14. 14

    How do I mock a service that returns promise in AngularJS Jasmine unit test?

  15. 15

    Unit testing an angular service method in jasmine

  16. 16

    Unit-Testing a filter with Jasmine in AngularJS

  17. 17

    Unit-testing slider directive with AngularJS + Jasmine

  18. 18

    Unit-Testing a filter with Jasmine in AngularJS

  19. 19

    Unit testing of Services / Factories - AngularJS - Jasmine

  20. 20

    ReferenceError : AngularJS is not defined on Karma with Jasmine unit testing

  21. 21

    Unit Testing AngularJS and PouchDB Service

  22. 22

    Unit testing the AngularJS $window service

  23. 23

    Angularjs unit testing service with promise

  24. 24

    Unit Testing AngularJS and PouchDB Service

  25. 25

    AngularJS Controller As Jasmine test with mock service with promise

  26. 26

    AngularJS testing with Jasmine unable to call "angular.mock.module" while using angular-mocks.js

  27. 27

    Unit test service that returns promise Angularjs Jasmine

  28. 28

    Unit test service that returns promise Angularjs Jasmine

  29. 29

    how to mock $state.params in jasmine unit testing

HotTag

Archive