1

I'm trying to test a simple call to my API, and I'm going round in circles trying to work out why it's failing.

I've simplified things a bit.

This would be the error for the test below:

Error: Unexpected request: GET /api/search?blah=something
No more request expected

Here is the test:

it('does what it should', function() {
  httpBackend.expectGET('/api/search?blah=something').respond(aTestResponse);
  scope.search();
  httpBackend.flush();

  // expectations here...
});

The search function in the controller:

function search() {
  myDataService.getSearchResults().query(mySearchParams, function(response) {
    // do stuff
  }
}

and the data service function:

function getSearchResults() {
  return $resource('/api/search', {
    param1: '@param1',
    param2: '@param2',
    ...etc
  });
}

Any suggestions would be really appreciated.

Edit - here is an edited, but more complete version of my spec file:

'use strict';

describe('Controller: BlahCtrl', function() {

  beforeEach(module('blahApp'));

  beforeEach(module(function($urlRouterProvider) {
    $urlRouterProvider.deferIntercept();
  }));

  var BlahCtrl;
  var scope;
  var rootScope;
  var httpBackend;

  beforeEach(inject(function($controller, $rootScope, $httpBackend) {
    httpBackend = $httpBackend;
    scope = $rootScope.$new();
    rootScope = $rootScope;
    BlahCtrl = $controller('BlahCtrl as vm', {
      $scope: scope
    });
    this.testResults = [
      {
        testProperty1: 'test-value-1-1',
        testProperty2: 'test-value-1-2',
        testProperty3: 'test-value-1-3'
      },
      {
        testProperty1: 'test-value-2-1',
        testProperty2: 'test-value-2-2',
        testProperty3: 'test-value-2-3'
      }
    ];
  }));

  beforeEach(function() {
    this.addMatchers({
      toEqualData: function(expected) {
        return angular.equals(this.actual, expected);
      }
    });
  });

  it('stores the search results', function() {
    httpBackend.expectGET('/api/search?blah=something').respond(this.testResults);

    scope.vm.doSearch();
    httpBackend.flush();

    // expectations here...
  });

});
9
  • it looks like your get call expects an object { param1: '@param1', param2: '@param2', ...etc }, try mocking your call in your test following the real expectations. Commented Oct 29, 2015 at 15:23
  • Thanks for the reply Jax. I'm a bit unsure on how I would do that, if you could maybe explain in a bit more detail? Commented Oct 29, 2015 at 15:31
  • first try this line: httpBackend.whenGET('/api/search?blah=something').respond(aTestResponse); just above your current expect get. Commented Oct 29, 2015 at 15:33
  • When using whenGet, I get the error TypeError: Cannot read property '$$nextSibling' of undefined Commented Oct 29, 2015 at 15:38
  • 1
    Yep, I do - thanks anyway :) Got it sorted now though, will post answer. Commented Oct 29, 2015 at 16:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.