kornfridge
kornfridge

Reputation: 5200

How to mock only a specific http resource (URL) in Angular

I'm working in a team split into front-end and back-end developers. Sometimes, the front-end wants to make some REST requests to a http address / REST-resource that isn't yet implemented.

In ngMockE2E there's a $httpBackend service that seems quite useful for this. The question is, how do I mock only some specific urls/rest resources?

I want most requests like e.g. GET /users.json to go through to the backend server as usual. But while waiting for e.g. GET /users/tommy/profile.json to get implemented (by the back-end guys), it would be nice to have some "pretend response", aka mocking, so we can keep working. How should one go about?

Thanks :)

Upvotes: 2

Views: 764

Answers (1)

Daniel Dykszak
Daniel Dykszak

Reputation: 276

try include this in your project:

app.config(function($provide) {
    $provide.decorator('$httpBackend', angular.mock.e2e.$httpBackendDecorator);
}).run(function($httpBackend) {
    // make sure we have access to
    $httpBackend.whenGET('*').passThrough();

    // here use your mocking data using $httpBackend
});

Upvotes: 1

Related Questions