How to write a karma-jasmine test case for $http.get in angularJS?
-
I have a 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 is my backend API*/ return $http.get(testUrl) .error(function(){ return; }) .then(function (response) { return response.data; }); } } })(); I call this in my controller as: testControllerFunction(); function testControllerFunction() { MyAppService.testFunction().then(function (response) { app.testResponse = response; //This my http response console.log(app.testResponse); }); } I am writing a karma test case for successful $http.get request in the 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 the backend testurl ', function () { MyAppService.testFunction(); http.expectGET(testUrl); }); }); But this does not seem to be working? Where did I go wrong? Thanks!
-
Answer:
You have to https://docs.angularjs.org/api/ngMock/service/$httpBackend#flushing-http-requests the $httpBackend it('should call the backend testurl ', function () { http.expectGET(testUrl); MyAppService.testFunction(); http.flush(); });
user3500989 at Stack Overflow Visit the source
Related Q & A:
- How To Write A Resume?Best solution by Yahoo! Answers
- How To Write A Letter?Best solution by Yahoo! Answers
- How to write a parser in C?Best solution by Stack Overflow
- How to write a persuasive letter to a bank?Best solution by wikihow.com
- How long should a urine drug test take to get results?Best solution by wiki.answers.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.