Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async Example for acceptance-test #163

Closed
olivierlesnicki opened this issue Apr 22, 2015 · 6 comments
Closed

Async Example for acceptance-test #163

olivierlesnicki opened this issue Apr 22, 2015 · 6 comments

Comments

@olivierlesnicki
Copy link

On the README.md it is stated that:

If an error is thrown in your promise or a promise within test becomes rejected, ember-qunit will fail the test. To assert that a promise should be rejected, you can "catch" the error and assert that you got there:

test('sometimes async gets rejected', function(assert){
  assert.expect(1);
  var myThing = MyThing.create()

  return myThing.exampleMethod().then(function(){
    assert.ok(false, "promise should not be fulfilled");
  })['catch'](function(err){
    assert.equal(err.message, "User not Authorized");
  });
});

While this works well for unit tests, I'm unable to find an equivalent workaround for acceptance test. I'd like to test scenarios in which my server calls fail (400, 500, etc..). But the error is always thrown and ember-qunit fail.

@olivierlesnicki
Copy link
Author

@olivierlesnicki
Copy link
Author

Our team member was able to workaround the issue by using:

    request.then(() => {}, (err) => {
      if (err.status === 400) {
        this.set('showErrorMessage', true);
      } else {
        throw error;
      }
    });

instead of

    request.catch((err) => {
      if (err.status === 400) {
        this.set('showErrorMessage', true);
      } else {
        throw error;
      }
    });

Does ember-qunit fail before the catch for rejected promises?

@olivierlesnicki
Copy link
Author

It seems ember-qunit fails whenever an error is not handled within the first .then()

rejectedPromise
  .then(successCallback, errorCallback)
  // the test doesn't fail
rejectedPromise
  .then(successCallback)
  // the test fails although the rejected promise has been handled
  .then(successCallback, errorCallback)
rejectedPromise
  .then(successCallback)
  // the test fails although the rejected promise has been handled
  .catch(errorCallback)

@voltidev
Copy link

@olivierlesnicki You can try to rewrite the original Ember.Test.adapter.exception like this:

/* jshint ignore:start */
import Ember from 'ember';
import { module, test } from 'qunit';
import startApp from 'payment-widget/tests/helpers/start-app';

let adapterException;

module('Acceptance | amount | server errors', {
  beforeEach() {
    this.application = startApp();

    // Ignore promise rejection.
    // Original exception will fail test on promise rejection.
    adapterException = Ember.Test.adapter.exception;
    Ember.Test.adapter.exception = () => null;
  },

  afterEach() {
    Ember.Test.adapter.exception = adapterException;
    Ember.run(this.application, 'destroy');
  }
});

test('visiting /:receiverId with invalid receiverId', async (assert) => {
  server.get('/payment-receivers/:id', null, 404);
  await visit(`/123`);
  assert.equal(currentRouteName(), 'not-found', 'it redirects to not-found route on 404 status');

  server.get('/payment-receivers/:id', null, 500);
  await visit(`/123`);
  assert.equal(currentRouteName(), 'error', 'it redirects to error route on 500 status');
});

If you'd like to hide console log errors you could also rewrite Ember.Logger.error the same way:

  beforeEach() {
    this.application = startApp();

    adapterException = Ember.Test.adapter.exception;
    emberLoggerError = Ember.Logger.error;

    Ember.Test.adapter.exception = () => null;
    Ember.Logger.error = () => null;
  },

  afterEach() {
    Ember.Test.adapter.exception = adapterException;
    Ember.Logger.error = emberLoggerError;

    Ember.run(this.application, 'destroy');
  }

@jonathansamines
Copy link

I am also experiencing the same trouble when trying to test "error" states conditions. The weird part is that my tests are only failing on [email protected] although the adapter has not changed since 1.13.x (or before) as you can see here. I´m still trying to figure out what is going on, although temporary replacing the Ember.Test.adapter.exception function has worked pretty well.

@Turbo87
Copy link
Member

Turbo87 commented May 13, 2019

closing this issue, as it should be resolved by the new testing APIs

@Turbo87 Turbo87 closed this as completed May 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants