10

I'm building an Ember CLI app (v0.2.3), and I have some unit tests that have been generated for me for the adapters and serializer in my app. The generated code looks like this:

// app/serializers/my-model-test.js

// Replace this with your real tests.
test('it serializes records', function (assert) {
  var record = this.subject();

  var serializedRecord = record.serialize();
  assert.ok(serializedRecord);
});

and

// app/adapter/application-test.js

// Replace this with your real tests.
test('it exists', function (assert) {
  var adapter = this.subject();
  assert.ok(adapter);
});

What do I put in these tests? I've built acceptance tests and unit tests for my models and components, but not sure what needs to go in these unit tests. Haven't been able to find documentation on building these unit tests, nor can I find any example applications on GH that have built these tests out.

user2688473
  • 633
  • 1
  • 6
  • 8

3 Answers3

14

If you want to create unit tests for your adapters and serializers, you should look at how ember data tests those themself. Basically, you can look at the test for the RESTSerializer etc. and use their technique.

Example serializer: https://github.com/emberjs/data/tree/master/tests/integration/serializers

The code that ember data uses to achieve this: https://github.com/emberjs/data/blob/master/tests/helpers/store.js

Steffen Brem
  • 1,738
  • 18
  • 29
7

I found it much easier to write an integration test for my custom serializer. I tried Steffans suggestion but I couldn't get it load anything other than the base JSONSerializer. The code I wrote that is working in Ember 1.13.8, Ember Data 1.13.15 is below.

import { moduleFor, test } from 'ember-qunit';

moduleFor('application', 'Integration | Serializer | application', {
  integration: true
});

test('Serializer normalizes correctly for basic single object', function(assert) {
  assert.expect(1);
  let store = this.container.lookup('service:store');

  var basicPeterJson = {
    id: 1,
    title: 'Mr',
    firstName: 'Peter',
    lastName: 'Parker'
  };

  var expectedHash = {
    data: {
      type: 'contact',
      id: 1,
      attributes: {
        title: 'Mr',
        firstName: 'Peter',
        lastName: 'Parker'
      },
      relationships: {}
    }
  };

  var contact = store.serializerFor('application').normalizeResponse(store, store.modelFor('contact'), basicPeterJson, 1);

  assert.deepEqual(contact, expectedHash);
});

I placed this in tests/integration/serializers/my-serializer-test.js

Adam Knights
  • 2,141
  • 1
  • 25
  • 48
  • 2
    I needed `moduleFor('serializer:application, ...)` for Ember to find the serializer. – meleyal Nov 17 '15 at 18:37
  • In the default serializer unit test, you have access to the store via `this.store()` since it uses `moduleForModel` instead of `moduleFor`. However, `serializerFor` is a private method. I find it easier testing serializers through the store with something like Pretender and treating serializers and adapters as "private" classes. – Animal Rights Feb 16 '16 at 17:42
2

Testing the adapter:

test('it has a url for creating a record', function (assert) {
  const url = this.subject().urlForCreateRecord('person', { firstName: 'Bob' });
  assert.equal(url, 'https://example.com/path/to/api');
});

Testing the serializer:

test('it serializes records', function (assert) {
  const serialized = this.subject({
    foo: 'bar',
  }).serialize();

  assert.equal(serialized.foo, 'bar');
});

For testing other serializer functions, I previously followed @Knightsy's integration test example and it worked for me. Many thanks! Then, I worked out that this can actually be simplified and unit tested (if you can call it that).

My test goes like this:

moduleForModel('person', 'Unit | Serializer | person', {
  needs: ['serializer:person'],
});

test('testing', function (assert) {
  const serializer = this.container.lookup('service:store').serializerFor('person');

  const payload = {
    id: 3,
  };

  const response = serializer.normalizeSingleResponse(null, null, payload, payload.id);

  assert.equal(response.data.id, 3);
});
Rimian
  • 36,864
  • 16
  • 117
  • 117