Ember integration testing hangs after visiting route

◇◆丶佛笑我妖孽 提交于 2019-12-24 13:32:56

问题


I am trying to do a simple integration test and I started from the integration example on the ember-cli website. Right now when I test in a browser (localhost:4200/tests), the follow case routes to where I expect, but then it just hangs and never does success or failure.

import Ember from "ember";
import { test } from 'ember-qunit';
import startApp from '../helpers/start-app';
var App;

module('Integration - Create Event', {
    setup: function() {
        App = startApp();
    },
    teardown: function() {
        Ember.run(App, App.destroy);
    }
});

test('check customers', function() {
    visit('/new');
    andThen(function() {
        fillIn('input#title', 'The Event Name');
        ok(true);
       // equal(find('.customers input[type="checkbox"]').length, 6, 'Customer checkboxes showing');

    });
});

Is there something I am doing wrong here? Or is there a different way to do it?

ember-cli 0.1.5 and ember 1.9.1

Edit:

    ENV.APP.LOG_ACTIVE_GENERATION = true;
    ENV.APP.LOG_TRANSITIONS = true;
    ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
    ENV.APP.LOG_VIEW_LOOKUPS = true;

Enabling logging shows that the transition completes, but andThen still never resolves or gets rejected.

Edit:

I think I've narrowed it down. I have a clock service that I am injecting into all controllers, but when I don't inject it at all, my test passes. I need the functionality the clock service provides, how can I still use it, but get my integration tests to work?

// services/clock.js
import Ember from 'ember';

export default Ember.Object.extend({
    pulse: Ember.computed.oneWay('_seconds').readOnly(),
    minutePulse: Ember.computed.oneWay('_minutes').readOnly(),
    tick: function () {
        var clock = this;
        Ember.run.later(function () {
            var seconds = clock.get('_seconds');
            var minutes = clock.get('_minutes');
            if (typeof seconds === 'number') {
                clock.set('_seconds', seconds + 1);
                if(Math.floor((seconds + 1) / 60) > minutes) {
                    clock.set('_minutes', minutes + 1);
                }
            }
        }, 1000);
    }.observes('_seconds').on('init'),
    _seconds: 0,
    _minutes: 0
});

An example project can be found at https://github.com/RyanHirsch/ember-test-example. If I remove the run.later the tests will pass.


回答1:


Thanks to teddyz in the #emberjs channel on freenode for helping me with this problem. The issue is that the async helper visit() waits for the Ember.run.later() to finish, but the way I have this structured, before it finishes it triggers another run.later. So the visit basically waits forever. The solution seems to be to use setTimeout with an Ember.run. Code is below.

import Ember from 'ember';

export default Ember.Object.extend({
    pulse: Ember.computed.oneWay('_seconds').readOnly(),
    minutePulse: Ember.computed.oneWay('_minutes').readOnly(),
    tick: function () {
        var clock = this;
        setTimeout(Ember.run.bind(clock, function () {
            var seconds = this.get('_seconds');
            var minutes = this.get('_minutes');
            if (typeof seconds === 'number') {
                this.set('_seconds', seconds + 1);
                if(Math.floor((seconds + 1) / 60) > minutes) {
                    this.set('_minutes', minutes + 1);
                }
            }
        }), clock.get('globalSettings.timeout'));
    }.observes('_seconds').on('init'),
    _seconds: 0,
    _minutes: 0
});

I've also created a globalSettings object per the discussion on the ember forums (http://discuss.emberjs.com/t/proper-way-to-handler-timers-w-ember-testing/4693/8) about the right way to handle timers and testing. This will allow for the adjusting of timeouts during testing (ie set them really low to test functionality and still maintain test execution speed)



来源:https://stackoverflow.com/questions/27851517/ember-integration-testing-hangs-after-visiting-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!