Testing backend API via $http in AngularJS/karma/jasmine tests?

后端 未结 1 349
抹茶落季
抹茶落季 2021-01-12 03:42

How do I test my API backend using AngularJS/karma/jasmine tests?

I have tried to create the smallest test-case showing my error:

echo_server.py



        
相关标签:
1条回答
  • 2021-01-12 04:21

    The mentioned testing stack is not intended for use this way. The request never gets dispatched because of the $httpMockBackend that has been decorated on top of your original $httpBackend.

    To allow requests to pass trough you either need to exclude angular-mocks.js, or specify that some urls should pass through like this:

    angular.module('yourModule').run(function ($httpBackend) {
        $httpBackend.whenGET(/.*/).passThrough();
    }
    

    Read the documentation for $httpMockBackend here

    Also, your test is synchronous, and the server response is asyncronous, so it will not work as expected.

    0 讨论(0)
提交回复
热议问题