Ember-cli:模型失败的关系单元测试

时间:2014-08-01 15:06:41

标签: unit-testing ember.js ember-data qunit

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

项目模型

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

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

这里添加了关于item和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

我错过了什么吗?

1 个答案:

答案 0 :(得分:9)

我现在正处于将旧的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' }
  ]);
});