1

HowTo properly test Angular's $http success and error promise with sinon.js? This is what I have so far (all in coffeescript):

Service Autobahn

drive: (vehicle) ->
  $http.post '/user/signin', vehicle
  .success ((data) ->
    # do stuff
    return data
  ).error
    throw Error (error)

Controller Streets

$scope.drive = (vehicle) ->
    $scope.data = Autobahn.drive(vehicle)

Unit Test w. Sinon

it 'should test $http callbacks', ->
  sinon.stub($scope, 'drive').yieldsTo 'success', callBackMockData
  $scope.drive vehicle, (data) ->
    expect($scope.data).to.be(callBackMockData)
    return
  return

Result

TypeError: 'undefined' is not a function (evaluating '(function(data) {
          return data;
        }).error(function(data) {
          throw Error(error);
        })')

Maybe stub is not the correct approach here as I want to really test the controller and service in one go. How can we test Angular's $http promises? The offical documentation uses httpBackEnd failing to provide details on how to test $http's callback chain.

Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147

1 Answers1

0

Don't use $http's success and error promises. $http is a promise by itself. Use .then().

See: Undefinied is not a function when using .success on a function that returns a promise

Community
  • 1
  • 1
Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147