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...
  });

});
J_P
  • 599
  • 1
  • 9
  • 20
  • 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. – Jax Oct 29 '15 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? – J_P Oct 29 '15 at 15:31
  • first try this line: httpBackend.whenGET('/api/search?blah=something').respond(aTestResponse); just above your current expect get. – Jax Oct 29 '15 at 15:33
  • When using `whenGet`, I get the error `TypeError: Cannot read property '$$nextSibling' of undefined` – J_P Oct 29 '15 at 15:38
  • can you post all you test js file pls – Jax Oct 29 '15 at 15:39
  • I've added a more complete edit of the spec file to my original question, thanks. – J_P Oct 29 '15 at 15:52
  • maybe a dumb question but do you have an get defined with api/search ? – 9ers Rule Oct 29 '15 at 16:06
  • 1
    Yep, I do - thanks anyway :) Got it sorted now though, will post answer. – J_P Oct 29 '15 at 16:11
  • Actually, I'm an idiot - I haven't fixed the issue after all. – J_P Oct 29 '15 at 16:24

0 Answers0