我正在创建一个 javascript 库并想使用 BDD,所以我正在尝试 mocha,但我无法让它工作。我希望在客户端上使用该库,所以我假设让它从可浏览的 url 运行是有意义的,在 Web 连接的上下文中,而不仅仅是来自 unc 路径的沙箱。
这是虚拟起点文件 test/test.foobar.js
var assert = chai.assert;
var foobar = {
sayHello: function() {
return 'Hello World!';
}
};
describe('Foobar', function() {
describe('#sayHello()', function() {
it('should work with assert', function() {
assert.equal(foobar.sayHello(), 'Hello World!');
});
});
});
这里是触发测试的html页面,test.html
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link rel="stylesheet" href="testing/mocha.css" />
<script src="testing/jquery.js"></script>
<script src="testing/mocha.js"></script>
<script>mocha.setup('bdd')</script>
<script src="testing/chai.js"></script>
<script src="test/test.foobar.js"></script>
<script> $(function() { mocha.run(); }) </script>
</head>
<body>
<div id="mocha"></div>
</body>
</html>
当我在 chrome 或 safari 中打开时
file:///Users/me/dev/sandbox/test.html
它按预期工作,测试通过没有错误
当我在 chrome 或 safari 中打开时
http://localhost/sandbox/test.html
出现以下错误,测试失败
Error: global leak detected: script1339700707078
at Runner.checkGlobals (http://localhost/sandbox/testing/mocha.js:3139:21)
at Runner.<anonymous> (http://localhost/sandbox/testing/mocha.js:3054:44)
at Runner.emit (http://localhost/sandbox/testing/mocha.js:235:20)
at http://localhost/sandbox/testing/mocha.js:3360:14
at Test.run (http://localhost/sandbox/testing/mocha.js:3003:5)
at Runner.runTest (http://localhost/sandbox/testing/mocha.js:3305:10)
at http://localhost/sandbox/testing/mocha.js:3349:12
at next (http://localhost/sandbox/testing/mocha.js:3233:14)
at http://localhost/sandbox/testing/mocha.js:3242:7
at next (http://localhost/sandbox/testing/mocha.js:3192:23)
有人可以解释一下,并提供更好的解决方案吗?
最佳答案
这是将 jQuery 与 mocha 结合使用时出现的问题。 jQuery 创建具有唯一 ID 的全局变量……在您的例子中是 script133...。最近发布的 mocha 1.2 你可以设置通配符忽略...
$(function(){
mocha
.globals([ 'script*' ]) // acceptable globals
.run();
});
确保您是最新的,并进行适当的配置。
关于javascript - 为什么浏览器中的 mocha 抛出从 url 而不是从 unc 路径检测到的全局泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11039811/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex
我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象