【问题标题】:Use of should.js with Coffeescript将 should.js 与 Coffeescript 一起使用
【发布时间】:2013-02-09 21:57:36
【问题描述】:

我想在我用 Coffeescript 编写的 Node.js 项目中将 should.js 和 mocha 一起使用。

在纯 Javascript 中,should.js 中的表达式类似于

(function(){
  throw new Error('fail');
}).should.throw();

现在我想在 Coffeescript 中编写相同的表达式。类似的东西

object.function().should.throw

但是编译成

object["function"]().should["throw"];

我的 Coffescript 代码哪里出错了?

【问题讨论】:

  • object.function 来自哪里?为什么叫它?
  • 这只是一个占位符。我使用的代码是testModel = new DBModel() testModel.get().should.throw()

标签: javascript node.js coffeescript should.js


【解决方案1】:

我不知道你为什么在你的咖啡脚本中写 object.function()。我认为编译成你的 JS 的咖啡脚本应该是:

(->
  throw new Error('fail')
).should.throw()

【讨论】:

  • 不,同样的问题。这编译为(function() { throw new Error('fail'); }).should["throw"]();
  • @rotespferd:这不正是你想要的吗?
【解决方案2】:

我使用的代码是testModel = new DBModel() testModel.get().should.throw()

据我从纯 JS 示例中看到的,您不应调用要测试的函数 - 它的结果可能没有 should 方法。改用这个:

testModel = new DBModel();
testModel.get.should.throw();

【讨论】:

  • 谢谢,你完全正确。 testModel.get.should.throw 工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 2013-08-24
  • 2013-07-10
  • 1970-01-01
  • 2013-11-28
  • 2012-11-12
相关资源
最近更新 更多