为什么我的测试中会出现“ Ajax授权失败”的情况

时间:2018-12-29 18:22:19

标签: javascript ember.js ember-cli qunit ember-qunit

我正在关注余烬指南,特别是我在services上。

我确定99.9%的代码正确无误-我正在手工复制,因为我相信这可以帮助我更全面地吸收它,但是如果出现任何问题,我会开始使用差异检查器来查看是否错字。据我所知,没有错字。

我编写的应用程序的性能与教程中的屏幕截图相同,唯一的错误是由于测试中没有断言而导致的皮棉错误(至今)。

在此单元之前,所有其他测试也已通过。但是现在我遇到了以前通过的失败测试。它们似乎全部源于对地图服务失败的存根调用。第一个失败的测试是integration/component/rental-listing-test.js

hooks.beforeEach(function() {
  this.rental = {
    image: 'fake.png',
    title: 'test-title',
    owner: 'test-owner',
    type: 'test-type',
    city: 'test-city',
    bedrooms: 3
  };
});
test('should display rental details', async function(assert) {
  await render(hbs`{{rental-listing rental=rental}}`);
  assert.equal(this.element.querySelector('.listing h3').textContent.trim(), 'test-title', 'Title: test-title');
  assert.equal(this.element.querySelector('.listing .owner').textContent.trim(), 'Owner: test-owner', 'Owner: test-owner');
});

如果我从rental-listing.hbs{{location-map location=rental.city}})中删除新行,从而阻止了地图的使用,则这些测试将再次通过(尽管使用该服务的组件的新测试有问题)

所以我在做我找不到的错事,或者emberjs.com的专家没有在本教程中提供完整的信息。我需要以某种方式存根地图服务吗?出现在.hbs文件中以通过上述测试?如果是这样,您为什么认为他们没有提及?

ETA断言:

Ajax authorization failed                      @ 273 ms
Source: Error: Ajax authorization failed
  at new EmberError (http://localhost:7357/assets/vendor.js:13635:31)
  at new AjaxError (http://localhost:7357/assets/vendor.js:116954:13)
  at new UnauthorizedError (http://localhost:7357/assets/vendor.js:116968:13)
  at Class._createCorrectError (http://localhost:7357/assets/vendor.js:117533:25)
  at Class.handleResponse (http://localhost:7357/assets/vendor.js:117528:25)
  at Object.jqXHR.done.fail (http://localhost:7357/assets/vendor.js:117380:41)
  at fire (http://localhost:7357/assets/vendor.js:3609:31)
  at Object.fireWith [as rejectWith] (http://localhost:7357/assets/vendor.js:3739:7)
  at done (http://localhost:7357/assets/vendor.js:9648:14)
  at XMLHttpRequest.<anonymous> (http://localhost:7357/assets/vendor.js:9889:9)

2 个答案:

答案 0 :(得分:1)

您不需要api密钥即可运行测试。您是否尝试过超级租赁仓库以查看是否存在相同问题? https://github.com/ember-learn/super-rentals

如果确实存在相同的问题,我们可能需要对本教程进行修复。

更新

我看到有问题的集成测试缺少存根映射服务定义。它在租赁仓库中,但在指南教程中未提及。有关代码,请参见https://github.com/ember-learn/super-rentals/blob/master/tests/integration/components/rental-listing-test.js。我已将此信息添加到更新指南的问题中:https://github.com/ember-learn/guides-source/issues/347

答案 1 :(得分:0)

所以我终于有时间去看它。问题在于,这是为外部地图服务设置的,以将环境变量用于API密钥。这就是为什么它可以很好地运行该应用程序(我使用KEY=value ember s启动该应用程序)但测试却没有的原因。只需使用KEY=value ember t -s即可通过这些测试。我只剩下棉绒问题了。

为了记录,这是应该放在教程本身中的内容,我不确定为什么我以前没有想到它。