Rotem B Rotem B - 2 months ago 12
AngularJS Question

Jasmine spec has no expectation

I have the following code:
(beforeEach)

spyOn(HttpService, 'post').and.callFake(function (url, paging, targetSpinner) {
return $q.when(_fakeServerResponse);
});


The test case:

it('should compare size', function () {
service.get({},'','').then(function (serviceResponse) {
expect(serviceResponse.x).toEqual(_fakeServerResponse.x);


and the get method:

return httpService.post(apiUrls).then(postComplete)


My problem is, as mentioned in the title: some why jasmine says that there are not expectations.

The service itself when not running a test is used:

myService.get(data, param1, param2).then(getComplete);


I will also add that when running the spec case, getComplete is never called, which is the source of the problem as I see it (yet I don't know why it doesn't get called).

Thanks

Answer

$q is asynchronous. The returned promise will only be resolved at the next scope digest. Your test should rather look like:

it('should compare size', inject(function($rootScope) {
  var actualX;
  service.get({},'','').then(function(serviceResponse) {
    actualX = serviceResponse.x;
  };

  $rootScope.$apply(); // that will actually resolve the promise

  expect(actualX).toEqual(_fakeServerResponse.x);
}));