Angularjs HTTP Request Eruption

So, I have already done quite a lot of research before asking this, and not one of them is what I want.

I have an application loaded with the angular module. The controller inside the module makes some HTTP requests. Now I am testing my application for ui and I need to mock HTTP requests. All the mocking that I read is done with jasmine, but I do not want to use it.

I want that whenever I open the application, all requests are tricked. I already tried angular mock and backend mocks, none of them work. And I do not want to pre-populate the elements of the area, because the code must be ready for deployment.

+4
source share
1 answer

If you want to mock your backend during development, just install angular-mocks in your main html file, add it as a dependency in your application ( angular.module('myApp', ['ngMockE2E']) ), and then scoff over the queries you need.

eg.

 angular.module('myApp') .controller('MainCtrl', function ($scope, $httpBackend, $http) { $httpBackend.whenGET('test').respond(200, {message: "Hello world"}); $http.get('test').then(function(response){ $scope.message = response.message //Hello world }) }); 

Be careful though adding ngMockE2E will require you to configure routes if you do this using AngularJS routing.

eg.

 angular.module('myApp', ['ngMockE2E']) .config(function ($routeProvider) { $routeProvider .when('/', { templateUrl: 'views/main.html', controller: 'MainCtrl' }) .otherwise({ redirectTo: '/' }); }) .run(function($httpBackend){ $httpBackend.whenGET('views/main.html').passThrough(); }) 
+7
source

Source: https://habr.com/ru/post/1502862/


All Articles