gpt4 book ai didi

unit-testing - Ember-cli : relationship unit test for model failing

转载 作者:行者123 更新时间:2023-12-03 07:46:02 25 4
gpt4 key购买 nike

我正在使用 Ember-cli 和 qunit 进行测试。

商品型号

import DS from 'ember-data';
var attr = DS.attr,
belongsTo = DS.belongsTo;

export default DS.Model.extend({
offer: belongsTo('offer'),
});

这里添加项目和报价模型之间关系的测试。

项目测试

import Ember from "ember";
import DS from "ember-data";
import { test, moduleForModel } from 'ember-qunit';

moduleForModel('item', 'Item Model', {
needs: ['model:item']
});

test('offer relationship', function() {
var relationships = Ember.get(App.Item, 'relationships');
deepEqual(relationships.get(App.Offer), [
{ name: 'offer', kind: 'belongsTo' }
]);
});

错误跟踪:

Died on test #1     at test (http://localhost:4200/assets/vendor.js:73836:13)
at eval (goodcity/tests/unit/item-test.js:44:5)
at requireModule (http://localhost:4200/assets/vendor.js:54:29)
at http://localhost:4200/assets/test-loader.js:14:29: App is not defined
Source:
ReferenceError: App is not defined
at Object.eval (goodcity/tests/unit/item-test.js:45:37)
at Object.wrapper (http://localhost:4200/assets/vendor.js:73824:31)
at Object.Test.run (http://localhost:4200/assets/qunit.js:203:18)
at http://localhost:4200/assets/qunit.js:361:10
at process (http://localhost:4200/assets/qunit.js:1453:24)
at http://localhost:4200/assets/qunit.js:479:5

我错过了什么吗?

最佳答案

我现在正在将旧的 Ember 应用程序转换为新的 ember-cli,并遇到了类似的情况。由于 Ember CLI 使用 ES6 模块语法,因此您无法直接访问 App 对象上的任何内容。

您需要从各自的模型文件中导入对象。

import Item from "<modulePrefix>/models/item";
import Offer from "<modulePrefix>/models/offer";

其次,你的 moduleForModel('item') 应该有需求:['model:offer']。

这是使用提供的文件通过测试。 (我使用:ember new stackoverflow)

import Ember from "ember";
import { test, moduleForModel } from 'ember-qunit';
/* Import Models */
import Item from "stackoverflow/models/item";
import Offer from "stackoverflow/models/offer";

moduleForModel('item', 'Item', {
// Item needs the offer model.
needs: ['model:offer']
});

test('offer relationship', function() {
/* For some reason this was necessary to prime the store. */
/* Without this line I get the error:
'undefined' is not an object (evaluating 'store.modelFor') */
var model = this.subject();

/* App.Item -> Item, App.Offer -> Offer */
var relationships = Ember.get(Item, 'relationships');
deepEqual(relationships.get(Offer), [
{ name: 'offer', kind: 'belongsTo' }
]);
});

关于unit-testing - Ember-cli : relationship unit test for model failing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25083206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com