如何为 Ember.js adapter/serializers 创建单元测试?
How to create unit tests for Ember.js adapter/serializers?
我正在构建一个 Ember CLI 应用程序 (v0.2.3),并且我已经为我的应用程序中的适配器和序列化程序生成了一些单元测试。生成的代码如下所示:
// 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);
});
和
// app/adapter/application-test.js
// Replace this with your real tests.
test('it exists', function (assert) {
var adapter = this.subject();
assert.ok(adapter);
});
我在这些测试中放了什么?我已经为我的模型和组件构建了验收测试和单元测试,但不确定这些单元测试需要做什么。无法找到有关构建这些单元测试的文档,我也无法在 GH 上找到任何已构建这些测试的示例应用程序。
如果你想为你的适配器和序列化器创建单元测试,你应该看看 ember 数据如何测试它们自己。基本上,您可以查看 RESTSerializer
等的测试并使用他们的技术。
示例序列化程序:https://github.com/emberjs/data/tree/master/tests/integration/serializers
ember数据用来实现这个的代码:https://github.com/emberjs/data/blob/master/tests/helpers/store.js
我发现为我的自定义序列化程序编写集成测试要容易得多。我尝试了 Steffans 的建议,但我无法让它加载除基本 JSONSerializer 之外的任何东西。我编写的代码在 Ember 1.13.8 中运行,Ember 数据 1.13.15 如下。
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);
});
我把它放在 tests/integration/serializers/my-serializer-test.js
正在测试适配器:
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');
});
正在测试序列化程序:
test('it serializes records', function (assert) {
const serialized = this.subject({
foo: 'bar',
}).serialize();
assert.equal(serialized.foo, 'bar');
});
为了测试其他序列化程序函数,我之前遵循了@Knightsy 的集成测试示例,它对我有用。非常感谢!然后,我发现这实际上可以被简化和单元测试(如果你可以这样称呼它的话)。
我的测试是这样的:
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);
});
我正在构建一个 Ember CLI 应用程序 (v0.2.3),并且我已经为我的应用程序中的适配器和序列化程序生成了一些单元测试。生成的代码如下所示:
// 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);
});
和
// app/adapter/application-test.js
// Replace this with your real tests.
test('it exists', function (assert) {
var adapter = this.subject();
assert.ok(adapter);
});
我在这些测试中放了什么?我已经为我的模型和组件构建了验收测试和单元测试,但不确定这些单元测试需要做什么。无法找到有关构建这些单元测试的文档,我也无法在 GH 上找到任何已构建这些测试的示例应用程序。
如果你想为你的适配器和序列化器创建单元测试,你应该看看 ember 数据如何测试它们自己。基本上,您可以查看 RESTSerializer
等的测试并使用他们的技术。
示例序列化程序:https://github.com/emberjs/data/tree/master/tests/integration/serializers
ember数据用来实现这个的代码:https://github.com/emberjs/data/blob/master/tests/helpers/store.js
我发现为我的自定义序列化程序编写集成测试要容易得多。我尝试了 Steffans 的建议,但我无法让它加载除基本 JSONSerializer 之外的任何东西。我编写的代码在 Ember 1.13.8 中运行,Ember 数据 1.13.15 如下。
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);
});
我把它放在 tests/integration/serializers/my-serializer-test.js
正在测试适配器:
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');
});
正在测试序列化程序:
test('it serializes records', function (assert) {
const serialized = this.subject({
foo: 'bar',
}).serialize();
assert.equal(serialized.foo, 'bar');
});
为了测试其他序列化程序函数,我之前遵循了@Knightsy 的集成测试示例,它对我有用。非常感谢!然后,我发现这实际上可以被简化和单元测试(如果你可以这样称呼它的话)。
我的测试是这样的:
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);
});