1

I've got an angular app going with some jasmine testing. I recently added a new method to make a query to elastic search within one of my services, it looks like this.

test:
    function(){
        return "Working";
    },

executeSearch:
    function(field, value, size, page_number){
        return service.executeRegExSearch(field, value, size, page_number);
    },

//new method
executeRegExSearch:
    function(field, value, size, page_number){
        //main search body, I know it works because
        //I am getting expected results in the browser
    }

And then in my jasmine tests, I've got something like this.

//initialization stuff
var $httpBackend;
var searchAPI;

beforeEach(inject(function($injector){
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
    $httpBackend = $injector.get('$httpBackend');
    searchAPI = $injector.get('searchAPI');
}));

it("is loaded properly", function(){
    expect(searchAPI.test() == "Working").toBe(true);  //passes
});

it("can make a request", function(){
    var field = "col_name";
    var value = "bb.*";  //this is matching in my browser/application
    var size = 10;
    var page_number = 1;

    var res;

    searchAPI.executeSearch(field, value, size, page_number).then(function(res){
        res = res;
        alert(JSON.stringify(res));
        done();
    });
    $httpBackend.flush();

});

But when I run, I get the error

Unexpected request: GET http://myserver/index-1/_search?source={"query":{"regexp":{"col_name":{"value":"bb.*"}}}}"&size=10&from=0 No more request expected in http://localhost:8081/js/angularjs/angular-mocks.js (line 1180)

I'm not sure about how to use the mocks, or am even aware that I was doing it. All I want to do is be able to run a suite of tests that make actual calls to my backend to confirm that things are being integrated properly... you know.. integration tests.

Anyone have any advice?

Zack
  • 13,454
  • 24
  • 75
  • 113

2 Answers2

1

I didn't want to do tests with Selenium stuff, so to test my app with real backend calls I used a modified version of angular-mocks

It works just like for unit-tests in Jasmine.

I'm using it with Jasmine 2.0, so a test looks like following :

it(' myTest', function (done) {
    _myService.apiCall()
        .then(function () {
            expect(true).toBeTruthy();
            done()
        });
});

NB: the done is needed because of the async call.

Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
0

If you want to do end to end e2e test for angular applications you definitely must use protractor (was created by the angular team), you could create your tests using jasmine and run them against your application, if you are just using unit testing I think the best way is to mock the httpBackend.

https://github.com/angular/protractor

https://docs.angularjs.org/guide/e2e-testing

Cesar Loachamin
  • 2,740
  • 4
  • 25
  • 33
  • alright, I want to do both, so I'll check out both of the links. Thanks! – Zack Sep 03 '14 at 16:35
  • In addition to using protractor for e2e teting, you can use ngMidway tester to make calls to a real backend form Jasmine tests. I'd recommend reading http://stackoverflow.com/questions/20864764/e2e-mock-httpbackend-doesnt-actually-passthrough-for-me and especially the plunker http://plnkr.co/edit/552UuwY6mQGyrMnJMqPx?p=preview – tapatron Sep 03 '14 at 20:27