jeudi 28 mai 2015

HowTo properly test Angular's $http success and error promise with sinon.js?

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.

Aucun commentaire:

Enregistrer un commentaire