Unit testing 如何为Ember.js适配器/序列化程序创建单元测试?
我正在构建一个Ember CLI应用程序(v0.2.3),我已经为我的应用程序中的适配器和序列化程序生成了一些单元测试。生成的代码如下所示:Unit testing 如何为Ember.js适配器/序列化程序创建单元测试?,unit-testing,ember.js,ember-cli,Unit Testing,Ember.js,Ember Cli,我正在构建一个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
// 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);
});
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);
});
及
我应该在这些测试中加入什么?我已经为我的模型和组件构建了验收测试和单元测试,但不确定在这些单元测试中需要做什么。还没有找到构建这些单元测试的文档,我也找不到在GH上构建这些测试的任何示例应用程序。如果您想为适配器和序列化程序创建单元测试,您应该看看ember data是如何自己测试这些测试的。基本上,您可以查看
RESTSerializer
等的测试并使用它们的技术
示例序列化程序:
ember data用于实现此目的的代码:我发现为自定义序列化程序编写集成测试要容易得多。我尝试了Steffans的建议,但除了基本JSONSerializer之外,我无法让它加载任何东西。下面是我在Ember 1.13.8和Ember Data 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的集成测试示例,它对我很有用。非常感谢!然后,我发现这实际上可以简化并进行单元测试(如果你可以这么说的话)
我的测试是这样的:
// 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);
});
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);
});
我需要
moduleFor('serializer:application,…)
让Ember找到序列化程序。在默认序列化程序单元测试中,您可以通过this.store()
访问存储,因为它使用moduleForModel
而不是moduleFor
。但是,serializerFor
是一个私有方法。我发现通过商店测试序列化程序更容易一些,比如使用假装器,并将序列化程序和适配器视为“私有”类。修正了链接!这是一个完美的评论。感谢您提供的链接和信息。问题是如何对序列化程序进行单元测试。这些是集成测试。然而,这是测试序列化程序的最简单方法。