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.
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.
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.
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.
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With