mercredi 12 août 2015

Issue with Async operations in Ember-Mocha Acceptance Tests

I'm battling with an issue where I receive the following error when running an acceptance test on my registration code:

Error: Uncaught Error: Assertion Failed: You have turned on testing mode, which disabled the run-loop's autorun. You will need to wrap any code with asynchronous side-effects in a run

I'm using the ember-cli-mocha package, which relies on the ember-mocha package.

Here's my test:

import {
  describe,
  it,
  beforeEach,
  afterEach
}
from 'mocha';
import {
  expect
}
from 'chai';
import sinon from 'sinon';
import Ember from 'ember';
import startApp from '../helpers/start-app';
import config from '../../config/environment';

describe('Acceptance: Registration', function() {
  var application;
  var server;

  ///////////////////////////////////////////////////
  /// Setup
  ///////////////////////////////////////////////////

  beforeEach(function() {

    // start the ember application
    application = startApp();

    // invalidate session if it exists
    invalidateSession();

    // create a fake server to intercept ajax requests
    server = sinon.fakeServer.create();
    server.respondImmediately = true;

    // simulate successful login
    server.respondWith('POST',
      config.APP.API_URL + '/auth/login', loginSuccessResponse());

    // simulate successful registration
    server.respondWith('POST',
      config.APP.API_URL + '/auth/register', registerSuccessResponse());
    });

  afterEach(function() {

    // restore an alterations to the login endpoint
    server.restore();

    // tear down the application
    Ember.run(application, 'destroy');

  });

  ///////////////////////////////////////////////////
  /// Tests
  ///////////////////////////////////////////////////

  it('can register with valid information', function(done) {

    visit('/signup');
    fillIn('#businessName', 'test');
    fillIn('#firstName', 'test');
    fillIn('#lastName', 'test');
    fillIn('#email', 'test@test.com');
    fillIn('#password', 'q1w2e3r4');
    click('#submit');

    andThen(function() {
      expect(currentRouteName()).to.equal('login');
      done();
    });
  });

And finally, here's the code that's running in the controller that's causing the hiccup:

signup: function() {
  const _this = this;
  const flashMessages = Ember.get(this, 'flashMessages');

  // clear leftover flash messages
  flashMessages.clearMessages();

  // perform validation on the form
  this.validate().then(function() {

    // create the business record
    var business = _this.store.createRecord('business', {
      'name': _this.get('businessName')
    });

    // create the businessAccount record
    var businessAccount = _this.store.createRecord('businessAccount', {});

    // register the user
    Ember.$.post('/auth/register', {
      businessName: _this.get('businessName'),
      firstName: _this.get('firstName'),
      lastName: _this.get('lastName'),
      email: _this.get('email'),
      password: _this.get('password')
    }).done(function(response) {
      flashMessages.success('Thanks for signing up! Check your email for a link to verify your account.');
      _this.transitionToRoute('login');
    }).fail(function(error) {
      flashMessages.warning(error);
    }); // register()

  }).catch(function() {
    // display any errors returned by the client-side validator
    var errors = _this.get('errors');
    for (var e in errors) {
      if (errors.hasOwnProperty(e) && errors[e].length > 0) {
        flashMessages.warning(errors[e]);
      }
    }
  }); // validate()
}

I realize that the call to Ember.$.post is asynchronous, but it was my understanding that the Ember test helpers, such as click() were of the asynchronous sort and andThen() would wait until any async operations have been completed. Is this not the case and should not be used with ember-cli-mocha?

Regardless, I'm pulling my hair out a bit and would appreciate any help before it's all gone :)

Thanks in advance!

Best, James




Aucun commentaire:

Enregistrer un commentaire