use*_*473 8 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 = record.serialize();
assert.ok(serializedRecord);
});
Run Code Online (Sandbox Code Playgroud)
和
// app/adapter/application-test.js
// Replace this with your real tests.
test('it exists', function (assert) {
var adapter = this.subject();
assert.ok(adapter);
});
Run Code Online (Sandbox Code Playgroud)
我在这些测试中放了什么?我为我的模型和组件构建了验收测试和单元测试,但不确定在这些单元测试中需要进行哪些测试.无法找到有关构建这些单元测试的文档,也无法在GH上找到构建这些测试的任何示例应用程序.
Ste*_*rem 14
如果要为适配器和序列化器创建单元测试,则应该看看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 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);
});
Run Code Online (Sandbox Code Playgroud)
我把它放在tests/integration/serializers/my-serializer-test.js中
| 归档时间: |
|
| 查看次数: |
4202 次 |
| 最近记录: |