httpbackend

AngularJS - Using ngMockE2E $httpBackend how can I delay a specific response?

坚强是说给别人听的谎言 提交于 2019-12-24 11:23:08
问题 I'd like to delay the response to the following whenGET: $httpBackend.whenGET(/^foobar/).respond(function () { return [200,{}]; }); However it seems impossible using $timeout to do this synchronously, so I'm not sure how to approach this? 回答1: If you want to delay only a specific response, than you may delay assignment of the reponse to scope property. If you wrap your call into your custom service method, than you may wrap the response into the promise and resolve it when needed: JS: angular

Activate $httpMock after angular app has been initialized

☆樱花仙子☆ 提交于 2019-12-24 05:46:13
问题 I want to be able to turn on and off a $httpBackend mock in my angularJS app. That means I want to inject the $httpBackend lazily/on demand. It would also be nice to be able to turn it on and off again. For example to provide input data for an AngularJS app that is being previewed from a CMS. The below code does only work if I move the ngMockE2E to an ordinary dependency, and inject $httpBackend to my factory the standard way. The code sets upp $httpBackend on all calls from a config file,

Delaying a response with $httpBackend

拥有回忆 提交于 2019-12-24 03:31:30
问题 In my view I have loading animation that is displayed until I receive a response from the API. //Displayed before we've received API response <p ng-if="vm.vehicles == null">Loading ...</p> //Displayed once we received response for the API <table ng-if="vm.vehicles"> <tr ng-repeat="vehicle.vm.vehicles">...</tr> To do my testing, I use the $httpBackend Angular module. Something like this: $httpBackend.whenGET('api/vehicles').respond({vehicles: [...]}); Problem I want to write a test to check

$httpBackend with request with query param

我与影子孤独终老i 提交于 2019-12-23 07:08:39
问题 $httpBackend.whenGET('/restpath/api/v1/books') .respond({// some data}); I get the following error Error: Unexpected request: GET /restpath/api/v1/books Expected GET /restpath/api/v1/books?limit=10&start=1 For the expectGET I have the following , and this creates dynamic query string. mostly the 'start' parameter, and the whenGET part, I am trying to server a dynamic content depending on the 'start' $httpBackend.expectGET('/restpath/api/v1/books?limit=10&start=1'); // the actual service goes

Get error when try to use jasmine and angular

折月煮酒 提交于 2019-12-19 05:44:50
问题 When I try to use $httpBackend.flush(); I get error TypeError: $browser.cookies is not a function. I can't find any information about this kind of error and any solutions. describe("someText", function() { var $httpBackend; var someManager; var authRequestHandler; var dataMockup = []; beforeEach(function(){ module('app'); inject(function($injector){ $httpBackend = $injector.get('$httpBackend'); someManager = $injector.get('someManager'); authRequestHandler = $httpBackend.when('GET', 'someUrl

How can I make $httpBackend insensitive to the order of URL query parameters?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 12:30:01
问题 I am using the Angular.js $httpBackend to test some services that wrap $http calls (this is in ngMock, not ngMockE2E). It seems that things like expect and when are sensitive to the order of URL query parameters. E.g. if I do $httpBackend.when('POST','/apiCall?X=1&Y=2').respond(/* ... */) or $httpBackend.expectPOST('/apiCall?X=1&Y=2') , I get URL mismatches if I have Y=2&X=1 in the URL instead of X=1&Y=2 . I want to write my tests in such a way that the service being tested will be free to

Getting “Unexpected request” error when running Karma unit test in an AngularJS app

人走茶凉 提交于 2019-12-10 10:35:19
问题 I'm trying to write a unit test for a controller which fetches article details using $http service. Controller: .controller('ArticleDetailCtrl',function($scope, Article, $routeParams, API_URL, ARTICLE_URL, $http, $sce){ $scope.url = API_URL + ARTICLE_URL + '/' + $routeParams.articleId; $http.get($scope.url).then(function(response) { //console.log(response.data); $scope.heading = response.data.Headline; $scope.rawBody = response.data.Body; $scope.body = $sce.trustAsHtml($scope.rawBody); $scope

How to actually reset $httpBackend expectations?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 19:08:45
问题 I've tried and tried to get this to work. The documentation is terse, at best: resetExpectations(); - Resets all request expectations, but preserves all backend definitions. Typically, you would call resetExpectations during a multiple-phase test when you want to reuse the same instance of $httpBackend mock. Every time my second request is called, my result always has the first result's data. Check out this fiddle http://jsfiddle.net/tbwn1gt0/2/ where I reset the expectations after the first

Getting “Unexpected request” error when running Karma unit test in an AngularJS app

。_饼干妹妹 提交于 2019-12-06 08:13:48
I'm trying to write a unit test for a controller which fetches article details using $http service. Controller: .controller('ArticleDetailCtrl',function($scope, Article, $routeParams, API_URL, ARTICLE_URL, $http, $sce){ $scope.url = API_URL + ARTICLE_URL + '/' + $routeParams.articleId; $http.get($scope.url).then(function(response) { //console.log(response.data); $scope.heading = response.data.Headline; $scope.rawBody = response.data.Body; $scope.body = $sce.trustAsHtml($scope.rawBody); $scope.image = response.data.Assets[0].URL; }); }); Unit test: 'use strict'; describe('Controller: Article',

Unit testing AngularJS with $httpBackend gives “Error: Unexpected Request”

纵饮孤独 提交于 2019-12-01 17:03:58
I'm gonna preface this question by saying I've already gotten a test to work for my "LoginCtrl" which performs almost the exact same task. I'm sure this is an issue of something really small I've overlooked. Thanks in advance for any help :) I've set the expected value/response for httpBackend for my test, but when I run httpBackend.flush() , it's as if I never set the expectation. Testing code: describe('user register', function () { var scope, RegisterCtrl, httpBackend, userJohnDoe, userJohnDoeRegistered, Restangular; // TEST beforeEach(module('LR.User.Register')); describe('RegisterCtrl',