vendredi 5 mai 2017

Ember; how to test behavior of a caught exception which wraps code in Ember.run

I am testing a service method that returns a promise; I want to verify that it catches an error, calls an error-reporting service, and then rethrows the error. I thought that since this error handler was a callback, I should run the catch code in Ember.run, but that is making my tests fail after upgrading from Ember 2.3 to Ember 2.13. Removing the Ember.run fixes the tests but I assume uses the autorun loop, which the guides discourage. What am I doing wrong?

service.js

// .... service boilerplate 
 doAsyncThing() {
    return get(this, 'someService').postV2Request().catch((e) => {
      // tests fail unless I remove Ember.run
      Ember.run(() => {
       let logError = new Error('foobar error');
       this.reportError(logError);
       throw(e);
      });
    });
  }
// ... service boilerplate

test.js

test('doAsyncThing reports an error if there is one', function(assert) {
  assert.expect(3);
  let done = assert.async();
  let deferred = RSVP.defer();
  let apiService = mock({
    postV2Request: sinon.stub().returns(deferred.promise)
  });
  let reportErrorStub = sinon.stub();
  let service = this.subject({
    apiService,
    reportError: reportErrorStub
  });

  service.doAsyncThing('foo', 'bar', 'baz').catch(() => {
    assert.ok(true, 'The exception is rethrown');
    assert.ok(reportErrorStub.called, 'reportError was called');
    assert.equal(reportErrorStub.args[0][0].message, 'foobar error', 'format of message is ok');
    done();
  });

  deferred.reject();
});




Aucun commentaire:

Enregistrer un commentaire