Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create unit tests for Ember.js adapter/serializers?

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.

like image 598
user2688473 Avatar asked May 01 '15 06:05

user2688473


People also ask

How do I run an ember integration test?

In order to integration test the Ember application, you need to run the app within your test framework. Set the root element of the application to an arbitrary element you know will exist. It is useful, as an aid to test-driven development, if the root element is visible while the tests run.

What is serializer in Ember JS?

In Ember Data, serializers format the data sent to and received from the backend store. By default, Ember Data serializes data using the JSON:API format. If your backend uses a different format, Ember Data allows you to customize the serializer or use a different serializer entirely.

Is unit testing necessary?

Unit testing is an essential part of the software development process that helps you ensure the high quality of your product: it allows developers to check the performance of each unit and prevent possible problems in advance.


1 Answers

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);
});
like image 121
Rimian Avatar answered Oct 23 '22 12:10

Rimian