2
votes

I want to test my models, and their integration with my REST API, using the Ember testing framework which ships with Ember CLI, but requests, using my RESTAdapter settings are not being made in my tests, within my model tests. As it stands, any call to save() on a model will cause all tests following it to not execute:

Here is a test I made to check interaction with the server (REST API):

test "store can be used to send data to server", ->
    store = @store()
    Ember.run ->
        cpanel = store.createRecord "item"
        cpanel.save().then((response) =>
            equal(response.status, 200)
        )

This completely blocks all tests following this one; furthermore, no requests are made to the server, when monitoring the Network tab in Chrome dev tools:

following tests fail

A friend advised me to use the QUnit Async Helper, but when using that, I find that this.store() is undefined (perhaps the Ember QUnit Adapter decided to leave out support for async testing helpers?):

asyncTest "Async creates account on server", ->
    expect(2)
    store = @store()
    Ember.run =>
        account = store.createRecord("account", {
            siteName: "sample account"
            url: "http://url.com"
        })
        account.save().then((response) =>
            equal(response.status, 200)
            ok account
            start()
        )

async testing fails in model test in ember cli

How can I write an async test for my individual models, and test their integration with my REST API using the Ember QUnit framework in Ember CLI without having to write integration tests?

1

1 Answers

2
votes

I'm not sure where status is coming from, the promise of save returns the record, not the response from server.

In order to use start, you must use stop first.

stop(); someAsyncCall(function(){ start(); });

And the store is only injected into routes and controllers, and isn't in scope of your tests. You'll need to use the container to get the store.

store = App.__container__.lookup('store:main');

It'd look something like this:

test("save a record", function(){
  var record,
      store = App.__container__.lookup('store:main');
  stop();
  Em.run(function(){
    record = store.createRecord('color', {color:'green'});
    record.save().then(function(){
      start();
      equal(record.get('id'), 1);
    });
  });
});

Example: http://emberjs.jsbin.com/wipo/49/edit