AngularJS, Mocha, Karma. testing controller, mocking service promise

han4wluc

I'm using AngularJS, Karma, Mocha, Chai, Chai-as-promised, Sinon.
I'm trying the controller below. I am having trouble mocking the service User and test the .then() sections in the controller.

Controller Code

.controller('SignupPhoneCtrl', function(User, $scope) {
  $scope.sendPhoneNumber = function(countryCode, phoneNumber){
    User.sendPhoneNumber(countryCode, phoneNumber)
    .then(function(result){
      if(result == "success"){
        //I WANT TO TEST THIS SECTION
        return "success";
      }
        //I WANT TO TEST THIS SECTION
      return "fail";
    });
  }
})

ControllerTest Code

describe('Controller: Signup-phone', function() {
  var scope;
  var UserMock;
  var SignupPhoneCtrl;

  beforeEach(function() {
    module('starter.controllers');
  });

  beforeEach(inject(function($rootScope, $controller, $q){
    scope = $rootScope.$new();
    UserMock = {
      sendPhoneNumber: function(countryCode, phoneNumber){
        var deferred = $q.defer();
        // console.log("called"); //This part is being called
        deferred.resolve('foo');
        return deferred.promise;
      }
    };
    SignupPhoneCtrl = $controller("SignupPhoneCtrl", {$scope: scope, User: UserMock});
  }))

  it('should return asdf', function(){
    scope.$digest();

    //WHAT SHOULD I WRITE HERE?

  })
});


I have tried the below but the assertion part is not being called.

scope.sendPhoneNumber(12, 53452123).then(function(result){
  result.should.equal("fail");  //THIS SECTION IS NOT CALLED
})

I have tried the below, but it gives error: 'undefined' is not a function (evaluating 'promise.then.bind(promise)'

scope.sendPhoneNumber(12, 53452123).should.eventually.equal("fail");

sendPhoneNumber(12, 53452123) returns Object{$$state: Object{status: 0}}


I tried to add sinon.spy(UserMock, 'sendPhoneNumber'), but it makes no difference.


Phil

The problem is, $scope.sendPhoneNumber doesn't return anything. Try

return User.sendPhoneNumber(...

in your controller method.


Also, if you're just going to instantly resolve the promise in your mock, I've found this works nicely and saves you setting up a deferred object

UserMock = {
  sendPhoneNumber: function(countryCode, phoneNumber){
    return $q.when('foo');
  }
};

That being said, you probably want to be able to control the resolved value in each test so this makes more sense...

var scope;
var UserMock;
var SignupPhoneCtrl;
var deferred;

// snip

inject(function($rootScope, $controller, $q) {
    deferred = $q.defer();
    UserMock = {
        sendPhoneNumber: function() {
            return deferred.promise;
        }
    };
});

// snip

it('tests for success', inject(function($rootScope) {
    deferred.resolve('success');

    scope.sendPhoneNumber(...).then(...);

    $rootScope.$apply();
}));

it('tests for failure', inject(function($rootScope) {
    deferred.resolve('not success');

    scope.sendPhoneNumber(...).then(...);

    $rootScope.$apply();
}));    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS, Mocha, Karma. testing controller, mocking service promise

From Dev

AngularJS Testing Controller with Karma

From Dev

Mocking a service with a promise, for testing within another service

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

Error initializing controller while testing angularjs with karma

From Dev

Angularjs unit testing service with promise

From Dev

Async testing with Karma and Mocha

From Dev

Angular's $interval service not found when testing using karma and mocha

From Dev

Angular.js promise not resolving when unit testing service with karma

From Dev

Angular and Karma testing a service that uses $resource and returns a promise

From Dev

Angular.js promise not resolving when unit testing service with karma

From Dev

Unit testing $q promise within Angular service - Karma, Jasmine

From Dev

angularjs 1 and jasmine, service promise testing

From Dev

TypeError: Cannot read property 'then' of undefined in testing AngularJS controller with Karma

From Dev

getting error when testing a controller using karma in angularjs

From Dev

Testing angular controller in Karma

From Dev

AngularJS and QUnit: Testing Controller with Service Dependency

From Dev

Difficulty in testing Angularjs controller with service as its dependency

From Dev

AngularJS Controller As Jasmine test with mock service with promise

From Dev

Resolving a promise in a service/factory vs in a controller with AngularJS

From Dev

service promise not resolving with karma

From Dev

service promise not resolving with karma

From Dev

AngularJS Karma Unit Testing

From Dev

Testing/Mocking Workers in Controller Specs

From Dev

Testing rejected promise in Mocha/Chai

From Dev

Testing Promise-chains with Mocha

From Java

Karma vs testing framework Jasmine, Mocha, QUnit

From Dev

Mocking Module Dependencies in Karma/Jasmine for AngularJS

Related Related

  1. 1

    AngularJS, Mocha, Karma. testing controller, mocking service promise

  2. 2

    AngularJS Testing Controller with Karma

  3. 3

    Mocking a service with a promise, for testing within another service

  4. 4

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

  5. 5

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

  6. 6

    Error initializing controller while testing angularjs with karma

  7. 7

    Angularjs unit testing service with promise

  8. 8

    Async testing with Karma and Mocha

  9. 9

    Angular's $interval service not found when testing using karma and mocha

  10. 10

    Angular.js promise not resolving when unit testing service with karma

  11. 11

    Angular and Karma testing a service that uses $resource and returns a promise

  12. 12

    Angular.js promise not resolving when unit testing service with karma

  13. 13

    Unit testing $q promise within Angular service - Karma, Jasmine

  14. 14

    angularjs 1 and jasmine, service promise testing

  15. 15

    TypeError: Cannot read property 'then' of undefined in testing AngularJS controller with Karma

  16. 16

    getting error when testing a controller using karma in angularjs

  17. 17

    Testing angular controller in Karma

  18. 18

    AngularJS and QUnit: Testing Controller with Service Dependency

  19. 19

    Difficulty in testing Angularjs controller with service as its dependency

  20. 20

    AngularJS Controller As Jasmine test with mock service with promise

  21. 21

    Resolving a promise in a service/factory vs in a controller with AngularJS

  22. 22

    service promise not resolving with karma

  23. 23

    service promise not resolving with karma

  24. 24

    AngularJS Karma Unit Testing

  25. 25

    Testing/Mocking Workers in Controller Specs

  26. 26

    Testing rejected promise in Mocha/Chai

  27. 27

    Testing Promise-chains with Mocha

  28. 28

    Karma vs testing framework Jasmine, Mocha, QUnit

  29. 29

    Mocking Module Dependencies in Karma/Jasmine for AngularJS

HotTag

Archive