5

我在 ember-cli 应用程序中有这些模型:

var PuzzleRound = DS.Model.extend({
    year: DS.attr('number')
});

var Puzzle = DS.Model.extend({
    puzzleRounds: DS.hasMany('puzzleRound', {async: true})
});

这是我的测试tests/unit/models/puzzle-test.js

import {
    moduleForModel,
    test
} from 'ember-qunit';
import PuzzleRound from 'weather-roulette/models/puzzle-round';

moduleForModel('puzzle', 'Puzzle', {
    // Specify the other units that are required for this test.
    needs: ['model:puzzleRound']
});

test('it exists', function() {
    var model = this.subject();
    // var store = this.store();    
    ok(!!model);               
});

运行时出现此错误ember test

Attempting to register an unknown factory: `model:puzzleRound`

我正在使用 ember-cli 0.1.1、Ember.js 1.7.0、Ember Data 1.0.0-beta.11。有人有什么我可以尝试解决的吗?

4

2 回答 2

3

我刚刚用 ember-cli 0.0.44 尝试了这段代码,我得到了和你一样的错误。

puzzleRound我将模型路径的两个引用重命名为puzzle-round,然后您的测试为我通过了。所以:

DS.Model.extend({
  puzzleRounds: DS.hasMany('puzzle-round', {async: true})
});

moduleForModel('puzzle', 'Puzzle', {
  needs: ['model:puzzle-round']
});

我知道连字符样式比 camelCase 样式更受欢迎,但我不确定这何时成为强制性的。此要求可能特定于 ember-cli 或 ember-qunit。

于 2014-10-16T01:23:19.227 回答
0

我一直在寻找与此类似的解决方案,但没有看到任何提及我的解决方案,所以我想无论如何我都会在这里发布。这真的很简单:确保你引用的控制器确实在那里。

// my-ember-application/tests/unit/controllers/index/bar-test.js
moduleFor('controller:index/bar', 'My Bar Test', {
    beforeEach() { .. }
});

test('it exists', function (assert) { 
    assert.ok(true); 
});

此代码将引用此位置的控制器:

my-ember-application/app/controllers/index/bar.js

于 2016-08-23T22:06:47.833 回答