cbl
April 8, 2015, 1:47pm
1
Hi.
We’re using Pretender with QUnit tests with ember-cli.
Specifically, we want to check whether an action displays an error message in an error dialog on the page for some request.
We set Pretender to answer the request with status code 409 and that is working fine.
Alas, the test fails prematurely with two extra assertions having failed:
1. Error: Conflict@ 316 ms
Expected: true
Result: false
Diff: true false
2. TypeError: cyclic object value@ 319 ms
Expected: true
Result: false
Diff: true false
The cause seems to be that Ember itself handles the failing AJAX promise spitting out these errors when in “testing” mode.
Using RSVP.off('error', Ember.RSVP.onerrorDefault);
at the beginning of the test case disables this behaviour, but that function is not even documented and it feels a bit awkward.
Is there a better way to handle this scenario? How do you let the errors just pass through to the application?
Thanks!
FYI, for anyone that finds this later. See https://github.com/simplabs/ember-simple-auth/issues/439
The problem is with LoginControllerMixin and AuthenticationControllerMixin. They have been deprecated, so stop using them and you don’t have to use this hack to “fix” the problem.
toranb
September 28, 2015, 2:00pm
3
I’m not entirely certain the core team would love this but here is how I’ve modified the setup/teardown for acceptance tests so I can do xhr failure scenarios
module('Acceptance | Some Test File', {
beforeEach() {
application = startApp();
originalLoggerError = Ember.Logger.error;
originalTestAdapterException = Ember.Test.adapter.exception;
Ember.Logger.error = function() {};
Ember.Test.adapter.exception = function() {};
},
afterEach() {
Ember.Logger.error = originalLoggerError;
Ember.Test.adapter.exception = originalTestAdapterException;
Ember.run(application, 'destroy');
}
});
Next I handle the error application side using a simple onError hook (w/ my ajax wrapper class)
hash.method = method;
hash = this.configureAjaxDefaults(hash);
return new Ember.RSVP.Promise(function(resolve, reject) {
hash.success = function(json) {
return Ember.run(null, resolve, json);
};
hash.error = function(json, textStatus, errorThrown) {
if (json && json.then) {
json.then = null;
}
Ember.run(self, "onError", json, textStatus, errorThrown);
return Ember.run(null, reject, json);
};
Ember.$.ajax(hash);
});
},
configureAjaxDefaults: function(hash) {
hash.method = hash.method || "GET";
hash.dataType = hash.dataType || "json";
hash.cache = hash.cache || false;
if(!hash.contentType && hash.data) {
Then in my ember-cli app I setup the following initializer to “set” the onError hook
import PromiseMixin from 'ember-promise/mixins/promise';
import errorFunction from 'myApp/utilities/error-function';
export function initialize() {
PromiseMixin.onError = errorFunction;
}
export default {
name: 'global-error-handling',
initialize: initialize
};
And finally what my error handler looks like in code
var errorFunction = (xhr, textStatus, errorThrown, message) => {
if (xhr.status === 403) {
}
};
export default errorFunction;