Backbone.sync error response: local HTTP stub or sync double
0 reputation · 31 Oct 2023, 04:35 UTC
0 reputation · 31 Oct 2023, 04:35 UTC
The goal is to verify that a Backbone.Model save failure propagates through the persistence layer without calling production credentials. The test must exercise the configured endpoint while remaining credential-free.
Backbone.sync performs the REST request, and Model#save invokes the error callback on an HTTP failure. The team can either point the endpoint at a loopback stub returning a controlled 4xx or 5xx response, preserving the real transport, or replace Backbone.sync with a capturing test double that isolates application logic. The installed Backbone release and HTTP adapter may affect whether failures surface as callbacks, promise rejections, or both.
Should the integration suite assert error propagation through the real HTTP adapter using a local stub? Is a Backbone.sync double sufficient for validating application error handling? What failure contract should apply when server response bodies are not standardized?
37275 reputation · 31 Oct 2023, 06:23 UTC
When a Backbone.Model.save fails because the server returns an error status, the framework funnels that result through Backbone.sync and into the Model’s error callback or promise rejection. The choice between a local HTTP stub and a Backbone.sync test double determines whether the transport layer’s error‑normalization is exercised.
responseText and responseJSON carry the expected status code, and that no production credentials are contacted.responseText population are not simulated, so error shape may differ from a real browser or Node environment.Backbone.js versions prior to 1.3.0 return failures exclusively as callback invocations; 1.3.0 and later also reject the promise returned by save. When server response bodies are not standardized, the error callback receives the raw response object; asserting on status code and headers is more reliable than parsing error messages.
http module, or any lightweight fixture) at the model’s urlRoot, configured to respond with, e.g., 422 Unprocessable Entity and a JSON payload { error: 'validation failed' }.urlRoot pointing to localhost:.model.save(attributes) and attach both a success callback and an error callback, or use .then/.catch on the returned promise.status property matching 422, and responseText/responseJSON reflecting the stub’s payload.If the goal is only to confirm that the model forwards the correct attributes to the persistence layer and that downstream handlers receive the expected error shape, a double can capture the request payload and return a synthetic error. However, the double must mirror the adapter’s response format (Content‑Type: application/json, a JSON body) otherwise the model may throw a different error type.
Missing diagnostic detail: which Backbone.js version the test suite targets, and whether errors are observed through callbacks, promises, or both. This determines whether the verification plan should include promise rejection checks alongside callback inspection.
Use comments to ask for clarification. Post a solution as an answer.
7,830 reputation · 31 Oct 2023, 06:54 UTC
A useful distinction is that Backbone’s default sync passes the jqXHR object—not the parsed server body—as the second argument to options.error(model, response, options). When the stub returns an empty or non-JSON error body, response.responseText can be an empty string and responseJSON may be undefined, so assertions on response.status are more reliable than parsing the payload.
If the suite uses a Backbone.sync double, invoke options.error asynchronously (for example inside setTimeout) to better match real transport timing; synchronous invocation can pass in unit tests while failing against an actual XHR adapter.