I am working on a big angular application which needs to mock entire API in the browser (also called backend-less development). I will have lots of different sections and views in my application, each of them will have its own mock definitions.
I do know that I may use the ngMock
module's $httpBackend
service which enables me to mock an AJAX call. That's what I want. And I've found a working snippet at jsfiddle.
But the thing that I'm missing is how to split this into multiple files? As I said, my app will have hunderds of pages and possibly even more RESTful resources and I can't simply put this into one source file. This in as architectural question: what is the best approach (that is working, scalable and easy to maintain) to divide thousands of whenGET
and whenPOST
calls into different files that will simply mock the same API? How to separate mocks in terms of project file structure? Shall there be a separate .run()
method for each module in the app? Can I load mocks from JSON files?
I would appreciate an explanation as well as a demo
To make answering easier, below is the relevant part of the fiddle:
myApp.run(function ($httpBackend) {
var phones = [{name: 'phone1'}, {name: 'phone2'}];
$httpBackend.whenPOST('/phones').respond(function (method, url, data, headers) {
console.log('Received these data:', method, url, data, headers);
phones.push(angular.fromJson(data));
return [200, {}, {}];
});
$httpBackend.whenGET('/phones').respond(function (method, url, data) {
console.log("Getting phones");
return [200, phones, {}];
});
$httpBackend.whenGET(/\.html$/).passThrough();
});