【问题标题】:writing mocha test for ember for first time, visit function missing第一次为ember写mocha测试,缺少访问功能
【发布时间】:2013-09-24 14:19:34
【问题描述】:

我是第一次尝试学习摩卡测试。我写了一个简单的测试脚本,比如:

describe('Analytics Test Suite', function(){
//http://emberjs.com/guides/testing/integration/
before(function() {
    AS.rootElement = '#ember-application-container';
    AS.setupForTesting();
    AS.injectTestHelpers();
    AS.reset();
});

describe('visit analytics index page', function(){
    visit("/analytics").then(function() {
        it('should return -1 when the value is not present', function(){
            expect([1,2,3].indexOf(4)).to.be(-1);
            expect([1,2,3].indexOf(0)).to.be(-1);
        })
    });
})
});

但是我得到这个 js 错误:

ReferenceError:访问未定义

但是如果我将代码修改为:

describe('Analytics Test Suite', function(){

AS.rootElement = '#ember-application-container';
AS.setupForTesting();
AS.injectTestHelpers();
AS.reset();


describe('visit analytics index page', function(){
    visit("/analytics").then(function() {
        it('should return -1 when the value is not present', function(){
            expect([1,2,3].indexOf(4)).to.be(-1);
            expect([1,2,3].indexOf(0)).to.be(-1);
        })
    });
})
});

我收到以下错误: TypeError: app._container_.lookup(...) 未定义

在第一个场景中,访问函数似乎丢失了,当将初始化代码放在之前的之外时,该函数得到了解决。但后来我得到类型错误,我认为它应该寻找 AS._container_lookup,但它正在寻找应用程序命名空间。我用的是ember调试版http://builds.emberjs.com/tags/v1.0.0/ember.js

您的帮助将不胜感激。我还添加了 jsbin http://jsbin.com/ILUbuy/2/

谢谢, 深

更新 我解决了添加此适配器的问题:https://github.com/teddyzeenny/ember-mocha-adapter

【问题讨论】:

  • 对于任何尝试设置相同设置的人,如果您使用的是ember-mocha-adapter,请确保删除任何与mocha 一起使用的done() 语句。
  • 请在回答您自己的问题时详细说明您的解决方案。 (是的,你可以这样做。)

标签: ember.js mocha.js


【解决方案1】:

我将尝试使用 jsbin 来跟进这篇文章的工作示例。当我第一次开始在 Firefox 中测试 ember 应用程序时,我注意到我总是会收到这个错误:

"before each" hook ‣
     router is undefined

但这在 chrome 中不会发生。我的领导刚刚发现我做错了什么或者我错过了什么。基本上 mocha 在执行 mocah.run() 时没有找到应该运行应用程序的 div,所以一个简单的解决方案是:

$(document).ready(function(){
    mocha.run();
});

我遇到的另一个错误是:

router.getHandler is not a function

原来这是因为我没有添加 App.reset();在我的 beforeEach 中。

在这种情况下,我必须测试一个 div 是否在点击时切换(使用 jquery UI,.tollge('slow'))。问题是测试会成功运行,但随后的测试总是会失败,我永远无法理解为什么会发生这种情况。我知道这是由于 jquery UI 的东西,因为如果我直接设置 div 的可见性而不使用花哨的效果,测试就会运行,所以如果你在内部使用 setTimeouts 的地方发生类似的事情,测试它做这样的事情:

 it("some test", function () {
    var context = this;            

    click(toggleButton).then(function () {
        Ember.run.later(context, function () {

        }, 1000);
        wait().then(function () {
            expect(DIV TO BE HIDDEN OR VISIBLE);
        });
    });
});

这个链接也很有帮助:http://instructure.github.io/blog/2014/01/24/ember-run-loop-and-tdd/

好的,这是一个工作示例:http://jsbin.com/opuJetOy/1/ 上面的链接在 chrome 中不起作用,似乎 chrome 不喜欢这样的链接: 'https://raw.github.com/....',但它可以在 Firefox 中使用。

【讨论】:

  • 如果您能找到解决问题的方法,请接受您的回答。它使其他访问此问题的人变得容易。
猜你喜欢
  • 1970-01-01
  • 2015-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-02
  • 1970-01-01
相关资源
最近更新 更多