In the process of developing my Angular.js application, I am incorporating RESTful services that utilize the $resource service. While this app will eventually connect to a Java Spring application, I am currently focusing on creating an isolated mock server on the client side to serve all necessary routes for testing purposes. Previously, I have successfully used Sinon.js to craft fake servers for other apps built with different MV* frameworks like Backbone.js.
It seems that Angular handles XHR requests differently compared to "standard" ajax calls utilized by JQuery/Backbone, making it challenging for Sinon to intercept and respond from the client side.
Although I attempted to use $httpBackend to simulate routes with predefined data, it seems this tool is primarily intended for unit testing rather than setting up a "staging environment" as needed in my case.
Here is how my current Sinon setup looks like, which effectively works with JQuery.ajax but struggles with Angular $resource or $http:
var server = sinon.fakeServer.create();
server.respondWith("GET", /mydata/gi, [200,
{ "Content-Type": "application/json" },
JSON.stringify({
data: "myData"
})
]);
server.autoRespond = true;
If anyone has insights into why this approach doesn't seamlessly integrate with Angular, or better yet, suggestions on setting up mocks for Angular applications, I would greatly appreciate the guidance!