1

I have some jasmine unit tests which are 'integration tests' that exercise some core components that are written as angular services. But my components won't call out to the web. My two tests to demonstrate the problem are like this...

[by the way I have a plug in that disables CORS]

  it('test http get', function () {

    httpGet('http://endpoint.json');

    function httpGet(theUrl) {
      var xmlHttp = null;

      xmlHttp = new XMLHttpRequest();
      xmlHttp.open("GET", theUrl, false);
      xmlHttp.send(null);
      return xmlHttp.responseText;
    }

  })

  it('test angular http get', inject(function($http) {

    $http.get('http://endpoint.json').
      success(function (data, status, headers, config) {

        console.log('success');

      }).
      error(function (data, status, headers, config) {

        console.log('failure');

      });

  }));

I know this is because of the $httpBackend mocking. However I a need to use the real implementation. I want to use the asynchronous features of jasmine to wait for the response or timeout but I just cannot figure out how to make angular actually make it's calls.

In other new jQuery calls work fine! :-)

Exitos
  • 29,230
  • 38
  • 123
  • 178

1 Answers1

0

$httpBackend only demonstrates the request, it is meant to save you time.

Why?

Assuming that you would like to test that your function is working, but the function depends on data retrieved from the server.

Instead of testing the honesty of the data, and the $http.get method which would take some time, it tests the honesty of the function, making sure that your function is doing what it is supposed to do.

Now, for creating async calls, you could either integrate with $timeout or $interval service,

OR

use Protractor: e2e - end to end testing tool that is testing your application "as a user", which is able to test honesty of values inside DOM elements, example:

Click <button id="a"></button> -> Verify that <p id="b"> has a value of hello world.

Protractor

Linial
  • 1,154
  • 9
  • 22
  • Hi thanks for the reply but I hate e2e tests they are slow and difficult to maintain. And if I change my view then I have to change my test, in my experience I would rather build an orthogonal app and test at the boundaries. Then maybe use 1/2 e2e tests maximum, to check the whole thing has deployed correctly. Anyway I blather, but this is why I am trying to stay well away from view testing with protractor. However, that said please enlighten me on using $timeout or $interval to make sure the angular calls actually happen? How would this work? – Exitos Nov 08 '14 at 18:11
  • It doesn't make the calls actually happen, it adds synthetic time to your test, but that might not be the perfect solution. Are you simply trying to test the honesty of $http service? Just to see if it's working? (BTW, Protractor is really really fast). – Linial Nov 08 '14 at 18:16
  • No I have an object called reservationCreator it is an angular service collection and it hits 3 web services and does a load of mapping logic. I want to integration test that module in isolation. Not drive it through a browser. People love browser testing and I tip my hat to them, for me they are no where near as fast, clean or as easy to read as direct http calls. I just need to test the domain logic and the data, not whether a button works. Luckily my modules are highly decoupled and I think I might just be able to inject jQuery at the seams to make this work....thanks for responding tho – Exitos Nov 08 '14 at 18:43
  • thanks I will have a look at this and see can I get it working might be what I need! – Exitos Nov 08 '14 at 19:23