# Setting up testing

**URL:** https://discuss.emberjs.com/t/setting-up-testing/7416
**Category:** Testing
**Created:** [February 26, 2015, 8:11pm UTC](https://discuss.emberjs.com/t/setting-up-testing/7416 "2015-02-26T20:11:46Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![joshpfosi](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@joshpfosi](https://discuss.emberjs.com/u/joshpfosi)
#### Post date: [February 26, 2015, 8:11pm UTC](https://discuss.emberjs.com/t/setting-up-testing/7416/1 "2015-02-26T20:11:46Z")

</div>

I am trying to write a simple Ember integration test and continue to get the frustrating run loop error despite using Ember.run. I’ve had a nightmare of a time trying to get this to work, if anyone could help me I’d be so grateful. Specifically, I can see the test sign in and begin loading the next page (as it should), but as soon as the test finishes I get that error. This is regarding the second test, the first passes (as nothing is async I believe).

```auto
import Ember from 'ember';
import startApp from 'jobs-tuftsdaily/tests/helpers/start-app';
import exists from 'jobs-tuftsdaily/tests/helpers/start-app';

var App;

module('Integration - Landing Page', {
    setup: function() {
        App = startApp();
    },
    teardown: function() {
       Ember.run(App, 'destroy');
    }
});

test('Should load content', function() {
  visit('/').then(function() {
    ok(exists("*"), "Found HTML!");
    ok(exists('label:eq(4)'), "Slug label on page");
  });
});

test('Should sign in test user', function() {
  Ember.run(function() {
    visit('/').andThen(function() {
      return fillIn("input[name=email]", "test@test.com");
    }).andThen(function() {
      return fillIn("input[type=password]", "password");
    }).andThen(function() {
      return click("button");
    }).andThen(function() {
      ok(1, "stupid test passed");
    });
  });
});

```

---

<div class="post-metadata">

### Author: ![joshpfosi](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@joshpfosi](https://discuss.emberjs.com/u/joshpfosi)
#### Post date: [February 26, 2015, 8:23pm UTC](https://discuss.emberjs.com/t/setting-up-testing/7416/2 "2015-02-26T20:23:03Z")

</div>

I also modified it with the same issue:

```auto
test('Should sign in test user', function() {
  expect(2);

  Ember.run(function() {
    visit('/');
    fillIn("input[name=email]", "test@test.com");
    fillIn("input[type=password]", "password");
    click("button");

    andThen(function() {
      ok(true, "stupid test passed");
    });
  });
});

```

---

<div class="post-metadata">

### Author: ![fr33dm](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/fr33dm/32/9989_2.png) [@fr33dm](https://discuss.emberjs.com/u/fr33dm)
#### Post date: [February 27, 2015, 4:54pm UTC](https://discuss.emberjs.com/t/setting-up-testing/7416/3 "2015-02-27T16:54:07Z")

</div>

You have to use Ember.run in your application code. Anywhere there is async code, wrap it in an Ember.run.

## Examples

With AJAX callbacks:

```
    App.User.login(data).then(function(response) {

      Ember.run(function() {
        self.handleLoginSuccess(response.user);
      });

    }, function(error) {

      Ember.run(function() {
        self.handleLoginError(error);
      });

    });

```

When setting a property:

```
Ember.run(function() {
    this.set('propertyName', newProperty);
});

```

It may seem annoying at first, but it makes sense once you understand what Ember has been doing for you behind the scenes with the Autorun. Ember has been wrapping your Async code automagically. In testing, that is disabled. It’s better to [learn more about the run loop](http://emberjs.com/guides/understanding-ember/run-loop/), and manage it yourself to make you app testable.

I hope this helps.

---

<div class="post-metadata">

### Author: ![joshpfosi](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@joshpfosi](https://discuss.emberjs.com/u/joshpfosi)
#### Post date: [March 1, 2015, 4:38am UTC](https://discuss.emberjs.com/t/setting-up-testing/7416/5 "2015-03-01T04:38:24Z")

</div>

I can’t seriously have to wrap ALL my async calls in Ember.run in my **application** code just to test my application? Or do I? That seems incredibly tedious?

---

<div class="post-metadata">

### Author: ![fr33dm](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/fr33dm/32/9989_2.png) [@fr33dm](https://discuss.emberjs.com/u/fr33dm)
#### Post date: [March 4, 2015, 6:06pm UTC](https://discuss.emberjs.com/t/setting-up-testing/7416/6 "2015-03-04T18:06:55Z")

</div>

I have it confirmed from [Toran Billups](https://github.com/toranb) who gave a talk on testing in Ember at [Ember Conf 2015](http://emberconf.com/schedule.html). You do need to wrap your app code with the Ember run like I had posted. You can find libraries like one at his repo that abstracts it.

[https://github.com/toranb/ember-promise/blob/master/addon/mixins/promise.js](https://github.com/toranb/ember-promise/blob/master/addon/mixins/promise.js)

---

<div class="post-metadata">

### Author: ![joshpfosi](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@joshpfosi](https://discuss.emberjs.com/u/joshpfosi)
#### Post date: [March 4, 2015, 6:20pm UTC](https://discuss.emberjs.com/t/setting-up-testing/7416/7 "2015-03-04T18:20:32Z")

</div>

I’ll try this out! Thank you for your help.
