unit testing - How to write a karma-jasmine test case for $http.get in angularJS? -


i have service:

(function () {     angular.module('app').service('myappservice', myappservice);     myappservice.$inject = ['$http', 'testurl'];     function myappservice($http, testurl) {         var service = {             testfunction: testfunction                 };         return service;         function testfunction() {             /*testurl backend api*/             return $http.get(testurl)                                 .error(function(){                     return;                 })                 .then(function (response) {                     return response.data;                 });         }     } })(); 

i call in controller as:

        testcontrollerfunction();         function testcontrollerfunction() {              myappservice.testfunction().then(function (response) {                 app.testresponse = response;  //this http response                 console.log(app.testresponse);             });         } 

i writing karma test case successful $http.get request in myappservice as:

describe('myappservice', function () {         var myappservice,http;         beforeeach(function() {             module('app');             inject(function ($injector) {                 myappservice = $injector.get('myappservice');                 testurl = $injector.get('testurl');                 http = $injector.get('$httpbackend');             });         });          it('should call backend testurl ', function () {             myappservice.testfunction();             http.expectget(testurl);         });     }); 

but not seem working? did go wrong? thanks!

you have flush $httpbackend

    it('should call backend testurl ', function () {         http.expectget(testurl);                     myappservice.testfunction();         http.flush();                }); 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -