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

I am building an Ember CLI application (v0.2.3), and I have some unit tests that were created for me for adapters and serializer in my application. The generated code is as follows:

// 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 did I put in these tests? I have developed acceptance tests and unit tests for my models and components, but I'm not sure what to do in these unit tests. The documentation for creating these unit tests could not be found, and I cannot find the sample GH applications that output these tests.

+8
unit-testing ember-cli
source share
3 answers

If you want to create unit tests for your adapters and serializers, you should see how this data is analyzed by yourself. Basically, you can watch the test for RESTSerializer , etc. And use their technique.

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

Code that uses ember data for this: https://github.com/emberjs/data/blob/master/tests/helpers/store.js

+14
source share

It was easier for me to write an integration test for my custom serializer. I tried Steffans' suggestion, but I could not get it to load anything but the base JSONSerializer. The code I wrote that works in Ember 1.13.8, Ember Data 1.13.15 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 posted this in tests / integrations / serializers / my-serializer-test.js

+6
source share

Adapter Testing:

 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'); }); 

Serializer Testing:

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

To test other serializer features, I previously followed the @Knightsy integration test example, and it worked for me. Many thanks! Then I decided that this could be simplified and checked for unity (if you can call it).

My test is as follows:

 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); }); 
0
source share

All Articles