Ember JS tutorial: TypeError: Cannot read property 'maps' of undefined

Ember JS tutorial: TypeError: Cannot read property 'maps' of undefined

我目前正在 运行 浏览他们网站上的官方 EmberJS 教程,我正在 this part。当我 运行 ember serve 时,应用程序本身一切正常,但问题是当我 运行 单元测试新服务时。我正在 运行ning ember test --server,我收到一个错误,我截取了以下屏幕截图:

单元测试代码:

import { moduleFor, test } from 'ember-qunit';
import Ember from 'ember';

const DUMMY_ELEMENT = {};

let MapUtilStub = Ember.Object.extend({
  createMap(element, location) {
    this.assert.ok(element, 'createMap called with element');
    this.assert.ok(location, 'createMap called with location');
    return DUMMY_ELEMENT;
  }
});

moduleFor('service:maps', 'Unit | Service | maps', {
  needs: ['util:google-maps']
});

test('should create a new map if one isnt cached for location', function (assert) {
  assert.expect(4);
  let stubMapUtil = MapUtilStub.create({ assert });
  let mapService = this.subject({ mapUtil: stubMapUtil });
  let element = mapService.getMapElement('San Francisco');
  assert.ok(element, 'element exists');
  assert.equal(element.className, 'map', 'element has class name of map');
});

test('should use existing map if one is cached for location', function (assert) {
  assert.expect(1);
  let stubCachedMaps = Ember.Object.create({
    sanFrancisco: DUMMY_ELEMENT
  });
  let mapService = this.subject({ cachedMaps: stubCachedMaps });
  let element = mapService.getMapElement('San Francisco');
  assert.equal(element, DUMMY_ELEMENT, 'element fetched from cache');
});

根据教程,我的理解是 this.subject({ cachedMaps: stubCachedMaps }) 会为我设置所有 maps 但服务本身似乎未定义,导致没有 属性 maps。这是正确的吗?可能是什么原因造成的?

来自 运行ning ember --version 的系统规格:

您发布的测试代码应该可以工作,所以问题一定出在其他地方。

您能否将其余代码与教程应用程序的完整版本进行比较?您可以克隆 repo @ https://github.com/ember-learn/super-rentals

所有测试 运行 都很好,包括地图服务。

所以 运行 变成了同样的东西。看起来在每次测试之间,前一个存根都被擦掉了。所以我在第二次测试中再次添加它并且效果很好:

test('should use existing map if one is cached for location', 
  function(assert) {
    assert.expect(1);
    let stubCachedMaps = Ember.Object.create({
      sanFrancisco: DUMMY_ELEMENT
    });
    let stubMapUtil = MapUtilStub.create({ assert });
    let mapService = this.subject({ mapUtil: stubMapUtil, cachedMaps: stubCachedMaps });
    let element = mapService.getMapElement('San Francisco');
    assert.equal(element, DUMMY_ELEMENT, 'element fetched from cache');
});

我运行也一样。 @cythrawll 的解决方案有效,但这不是 TypeError: Cannot read property 'maps' of undefined 错误的根本原因。

在教程中他们忘记添加

<script src="https://maps.googleapis.com/maps/api/js?v=3.22"></script>

test/index.html 文件中。 Issue 已报告。