質問

How do you test an error callback in an Angular controller like the following:

controller.js

  Note
    .query()
    .$promise
    .then(
      function( notes ) {
        // success
      },
      function() {
        // error
      }
    );

In my spec, I can put some random object in the call to queryDeferred.resolve() and test the success callback. But how do I test the error case?

spec.js

//= require spec_helper

describe( 'NoteListCtrl', function() {
  // edited for brevity...

  beforeEach( inject( function( $controller ) {

    NoteMock = {
      query: function() {
        queryDeferred = $q.defer();
        return { $promise: queryDeferred.promise };
      }
    }

    spyOn( NoteMock, 'query' ).andCallThrough();

  }));

  describe( 'Note.getNotes', function() {

    describe ( 'Note.query', function() {

      beforeEach( function() {
        $scope.getNotes();
        queryDeferred.resolve( ????? ); // What goes here?
        $rootScope.$apply();
      });

    });


  });

});
役に立ちましたか?

解決

Use reject:

queryDeferred.reject(someData);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top