English 中文(简体)
咖啡豆中的匿名功能如何测试
原标题:Anonymous function in Coffeescript how to test it

我知道,在Stackover流程中有很多关于这个问题的讨论,但我无法对此作出直接回答。 我不知道很多咖啡。

基本上,我有这种咖啡。

return42 = -> 42

当我编辑时,我就这样做了。


(function() {
  var return42;

  return42 = function() {
    return 42;
  };

}).call(this);

So the function it s wrapped in the anonymous function which it s not exposed to the world. So when I write this test


describe "Test number", ->
    it "is 42", ->
        expect(return42()).toBe 42

测试将失败,因为返回42()没有定义。 我如何解决这一问题。

非常感谢你:

最佳回答

你们需要一个全球变量,作为你们方案的切入点。 你可以做到这一点,把你的职责放在全球目标上,而不是让它发挥地方作用。 而是:

@return42 = -> 42

您:

(function() {
  this.return42 = function() {
    return 42;
  };
}).call(this);

如果你在浏览器上而不是在Node.js上这样做,那么将比照window<<>code>,而不是this,即使两者都是本案的全球目标,也是一种比照的比照。

问题回答

虽然吉米是正确的,但我要补充说,如果你不需要暴露这一功能,那么你就不必加以测试。 而是测试使用这一职能的公众人物。 只要你公开的公益物依赖你的私人实施,那么,如果私人功能失败,试验就应当失败。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.