测试时,store.query的一个实例永远不会解析

时间:2017-01-03 23:01:13

标签: javascript ember.js ember-data ember-testing ember-validations

我正在尝试为我的Ember应用程序编写验收CRUD测试。基本上,我有一个模型("客户"),我想在验收测试中创建,编辑和删除。创建客户时,我使用ember-validate进行异步验证。此验证使用store.query调用服务器。当我自己运行验收测试时,它运行正常。但是,当测试由另一个使用visit()帮助程序的测试进行时,测试将失败,因为异步验证永远不会解析,因此无法创建客户。对store.query的调用确实成功查询了服务器,并且得到了响应,但出于某种原因,then()catch()finally()挂钩永远不会得到调用。有没有人遇到过这种事情?

我在调试器中花了好几个小时跟着调用堆栈试图找到承诺被卡住的地方,但我没有太多运气。

这里有更多细节:

我的测试 - 更易于理解

import Ember from "ember";
import { module, test } from 'qunit';
import startApp from 'my-app/tests/helpers/start-app';

module('Acceptance | customer', {
    setup() {
        this.application = startApp();

        this.createCustomer = function (unique) {
            // The process of creating a customer...
            return wait();
        };
        this.editCustomer = function (unique) {
            // The process of editing a customer
            return wait();
        };
    },
    teardown() {
        Ember.run(this.application, 'destroy');
    }
});

// This test doesn't do anything except visit the root url. If I comment out or skip this test, the next test passes. Otherwise, the next test fails.
test(` loads`, () => {
    expect(1);

    visit('/');
    andThen(() => {
        ok(true);
    });
});

test('a customer can be managed', function (assert) {
    const UNIQUE = //Some method for getting a unique string;

    expect(3);

    // Go to the home page
    visit('/');

    // Create a customer
    andThen(() => this.createCustomer(UNIQUE));

    // Assert that creation was successful
    andThen(() => {
        assert.ok('Some criteria that determines that the customer was created');
    });

    // Edit the customer
    andThen(() => this.editCustomer(UNIQUE));

    // Assert that we successfully edited the customer
    andThen(() => {
        assert.ok('Some criteria that determines that the customer was edited');
    });
});

我的验证员 - 为了更容易理解而变得更通用

import Ember from 'ember';
import ValidationsMixin from 'some-mixin';

export default Ember.Object.extend(ValidationsMixin, {
    /**
     * The Ember data store.
     */
    store: Ember.inject.service('store'),

    /**
     * The customer.
     */
    customer: null,

    validations: {
        'customer.primaryDomain': {
            presence: true,

            format: \some-regex\,

            async() {
                let store = this.get('store');
                let domain = this.get('customer.primaryDomain');

                if (Ember.isEmpty(domain)) {
                    return Ember.RSVP.resolve(true);
                }

                // Validate that no other customers exist with the same domain
                const promise = store.query('customer', {
                    primaryDomainList: [domain]
                });

                // At this point, the promise object is created, but this .then() hook may never get called even though the ajax request was successful.
                promise.then(customers => {
                    // Some extra code that isn't relevant
                });

                return promise;
            }
        },

        'customer.companyName': {
            presence: true
        }
    }
});

0 个答案:

没有答案