gpt4 book ai didi

javascript - Ember Data 的商店 promise 在五次测试中不起作用

转载 作者:行者123 更新时间:2023-11-28 07:57:32 25 4
gpt4 key购买 nike

更新

下面的说明性代码的一些怪癖的一些上下文。 StoreProxy 作为一个模型存在,由 ApplicationRouter 创建,具有对存储的引用。这允许其他对象直接访问存储(对于单例、测试等)。示例:

MyApp.StoreProxy = DS.Model.extend();

MyApp.ApplicationRoute = U.Route.extend({
model: function () {
return this.store.createRecord('storeProxy');
}
});

在执行路由之前,StoreProxy 没有 store 属性。之后,确实如此。我只能假设这是因为一些 ember-data 魔法。

我很清楚你对此的 react 可能是“呃!不!你做错了!”。著名的。随着时间的推移,我们将从这里开始以正确的方式进行。也就是说,这就是代码现在所在的位置。那么,考虑到这一点,并考虑到这种获取当前存储的引用的方法,为什么下面的代码不调用它的接受或拒绝处理程序呢?

原始问题

我正在为 ember 编写 qUnit 单元测试。我正在使用夹具数据。对商店的 findAll 调用未解决或拒绝 promise 。

test('Find all in store', function() {
expect(1);
var findPromise;
findPromise = MyApp.StoreProxy.store.findAll('rule');
findPromise.then(function(result) {
console.log('yes');
ok(true);
}, function(error) {
console.log('no');
});
});

我尝试使用此问题中提到的异步测试: testing ember fixture data with quint但解决和拒绝从未被调用,因此测试无限期挂起。

我还尝试在我的代码周围放置 Ember.run 调用,以防出现奇怪的运行循环问题。但无济于事。

 asyncTest('Find all in store', 1, function() {
var findPromise;
Ember.run(function() {
findPromise = MyApp.StoreProxy.store.findAll('rule');
findPromise.then(function(result) {
console.log('yes');
ok(true);
start();
}, function(error) {
console.log('no');
start();
});
});
});

当我正常运行应用程序(或没有夹具适配器)时,我正在测试的代码运行良好,所以感觉就像测试环境一样。

有什么想法可以尝试吗?我被难住了。

最佳答案

您编写异步测试的方式不正确。查看QUnit's page on async testing 。您的测试应该如下所示:

asyncTest('Find all in store', function() {
var findPromise = ...;

findPromise.then(function(result) {
start();
ok(result);
}, function() {
start();
ok(false);
});
});

具体:

  1. 您在 asyncTest 函数中添加了一个额外的参数,这可能会导致测试根本无法运行。
  2. 您正在使用 Ember.Application.store,这不是您访问商店的方式(甚至可能不是有效的商店)。我不确定您的背景是什么,但您应该从其他地方获取您的商店。
  3. 您将 start() 调用放在了断言之后,而它们本应位于断言之前。

关于javascript - Ember Data 的商店 promise 在五次测试中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25880057/

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