Let's say I have this service. PhotoService.getPhotos returns an $http 
promise:

angular.module('app').factory('PhotoService', function($http) {
  return {
    getPhotos: function() {
      return $http({
        method: 'GET',
        url: '/photos',
        params: {
          showAuthor: true,
          showSizes: false
        }
      });
    }
  };
});


I want to write a unit test that checks for the following conditions:

1. when PhotoService.getPhotos() is called, a 'GET' request to 
'/photos.json' is made.
2. when PhotoService.getPhotos() is called, two params are passed called 
showAuthor and showSizes.

describe('photo-service', function() {
  var photoService, $httpBackend;

  beforeEach(module('app'));

  beforeEach(function(PhotoService, _$httpBackend_) {
    photoService = PhotoService;
    $httpBackend = _$httpBackend_;
    
$httpBackend.whenGET('/photos?showAuthor=true&showSizes=false').respond(200, 
'great');
  });

  it('should make a GET request when PhotoService.getPhotos is called', 
function() {
    $httpBackend.expectGET('/photos?showAuthor=true&showSizes=false');
    $httpBackend.flush();
  });

  it('should have showAuthor and showSies as query params', function() {
    // no idea how to get the URL actually called in the real service here?
  });

  afterEach(function() {
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
  });

})



The first test passes just fine and if I change 'GET' to 'POST' in the 
service, the test appropriately fails. Here are my questions:

*1. The first test passes yet I didn't have to call 
PhotoServices.getPhotos() in my test case? I'm used to mocking out $q 
promises where I callThrough() using a Jasmine spy on a method.*


*2. How do I test which query params are being sent in a request?*


-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to