本文介绍了使用QUnit和Karma测试Emberjs应用程序由于“ReferenceError:Faye未定义”而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试EmberJs应用程序与Qunit和业力赛跑者,它的工作都很好。
然后,我不得不将faye整合到应用程序中,然后运行我的测试套件,它显示以下错误和崩溃:

错误被抛出,我在emberjs中定义客户端

  client = new Faye.Client(uri); 

尽管这在开发,分期但不在测试中起作用。
Overhere, uri =http:// localhost:9292 / faye



faye.js包含在vendor.js(单个js文件,其中包含所有js插件,包括ember.js和ember-data.js本身),它在app.js之前加载(上面存在的文件)

解决方案

这个奇怪行为的原因与Faye中的以下行有关:



if(typeof module!=='undefined')
module.exports = Faye;
else if(typeof window!=='undefined')
window.Faye = Faye;



资料来源:

所以如果模块未定义(意思是定义),那么 module.exports 将设置对象,如果不是, window.Faye 将被设置。

如果使用调试器并在该行上设置断点,那么该模块实际上是定义,为什么?这是一个QUnit全局帮助方法!

为什么Faye这样做?我猜是因为它可以在NodeJS环境中运行(您可以阅读更多关于module.exports )。

如我所见,您有两种可能的解决方案:




  • 在QUnit之前需要Faye。

  • Patch Faye忽略窗口中模块的存在。



在使用与QUnit。

在QUnit看起来像一个坏主意之前,让Teaspoon需要Faye,而且可能会在丑陋的黑客。

我已经结束了修补Faye,使其跳过模块检查,也不是那么美丽,但我相信这是合理的。


I Was testing EmberJs application with Qunit and karma runner and it was working all good.Then I had to integrate faye into the application which went well but then on running my test suite, it shows following error and crashes:

The error is thrown where, I am defining the client in emberjs

client = new Faye.Client(uri);

Though this works in development, staging but not in testing.Overhere, uri = "http://localhost:9292/faye"

faye.js is included in vendor.js(single js file which have all the js plugins including ember.js and ember-data.js itself) which is loaded before app.js(file where above line exists)

解决方案

The reason of that weird behavior is related to the following lines in Faye:

if (typeof module !== 'undefined') module.exports = Faye;else if (typeof window !== 'undefined') window.Faye = Faye;

Source: https://github.com/faye/faye/blob/master/javascript/faye.js#L143
So if module is not undefined(meaning it is defined) then module.exports will be set object, if not, window.Faye will be set.
If you use a debugger and set a breakpoint on that line you will that module is actually defined, why? it is a QUnit global helper method!
Why Faye does this? I guess because it can be run in a NodeJS environment (you can read more about module.exports here).
As I see it you have 2 possible solutions:

  • Require Faye before QUnit.
  • Patch Faye to ignore the existence of module in window.

I've encountered the same behavior while using Teaspoon with QUnit.
Making Teaspoon require Faye before QUnit seems like a bad idea and will be probably in ugly hack.
I've ended up with patching Faye to make it skip the module check, not so beautiful either, but I believe this is reasonable.

这篇关于使用QUnit和Karma测试Emberjs应用程序由于“ReferenceError:Faye未定义”而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 16:03