在Javascript / Coffeescript中如何在测试中模拟出模块的公共变量? [英] In Javascript/Coffeescript how can I mock out a module's public variable in a test?

查看:224
本文介绍了在Javascript / Coffeescript中如何在测试中模拟出模块的公共变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚了解了模块模式。我写了一些代码已经足够复杂,我想测试一个功能。问题是,我不知道如何模拟出一个值。以下是coffeescript,但我会把生成的javascript下面,这样我可以得到更多的帮助。这是我的测试,试图模拟出一个名为 state 的字段,但它总是打印未定义:

I just learned about the module pattern. I've written some code that's gotten sufficiently complex that I want to test a feature. The problem is, I can't figure out how to mock out a value. What follows is coffeescript, but I'll put the generated javascript below so I can get more help. Here's my test that attempts to mock out a field named state, but it always prints "undefined":

root = exports ? this
root.test3 = (->
  root.test2.state = "abc"
  root.test2.doSomething()

  return {}
)()

这里的test2是为了这个问题的目的可以认为是生产代码:

And here's test2 which for the purpose of this question can be considered the production code:

root = exports ? this
root.test2 = (->
  state = undefined

  doSomething = -> alert state

  return {
    state: state
    doSomething: doSomething
  }
)()

这里是我运行的html,但我不认为这很重要:

Here's the html I'm running this in, but I don't think it matters:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>TEST2</title>

    <script src="test2.js" type="text/javascript"></script>
    <script src="test3.js" type="text/javascript"></script>

    <style>
        canvas {
            border: 1px solid black;
        }
    </style>

</head>

<body>


<h1>This is a test</h1>

</body>
</html>

test2在Javascript中:

test2 in Javascript:

var root;

root = typeof exports !== "undefined" && exports !== null ? exports : this;

root.test2 = (function() {
  var doSomething, state;
  state = void 0;
  doSomething = function() {
    return alert(state);
  };
  return {
    state: state,
    doSomething: doSomething
  };
})();

test3在Javascript中:

test3 in Javascript:

var root;

root = typeof exports !== "undefined" && exports !== null ? exports : this;

root.test3 = (function() {
  root.test2.state = "abc";
  root.test2.doSomething();
  return {};
})();



如上所述,运行此警报 undefined 。我想要输出abc。

As I said, running this alerts undefined. I want it to output "abc". How can I do this?

推荐答案

您想在这种情况下使用this,

You want to use 'this' in that case,

doSomething = function() {
    return alert(this.state);
};

在你的代码中,'state'指的是未定义的变量然后返回一个包含新的'state'属性的对象。你不需要第一个'state'变量,而只需要返回的对象中的属性。

In your code, 'state' refers to the variable (via closure) which is undefined. You then return an object containing a new 'state' property. You don't need the first 'state' variable, but only the property in your returned object.

'this'在doSomething中指的是任何对象。在test3中,doSomething引用返回的对象,'state'设置为undefined。一旦在test3中设置状态,对象的'state'属性就不再与test2中的私有变量'state'相同 - 它在test3中被覆盖。

'this' in doSomething refers to whatever object it is a part of. In test3, doSomething refers to the returned object with 'state' set to undefined. Once you set state in test3, the object's 'state' property is no longer the same as the private variable 'state' in test2-- it is overwritten in test3.

基本上,你不需要你想要的效果模块模式,有一个更容易的方法:

Essentially, you don't need the module pattern for the effect you desire, there's a much easier way:

root.test2 = {
    state: void 0,
    doSomething: function() {
        return alert(this.state);
    }
};

http://jsfiddle.net/rMJs5/

UPDATE ::我忘记了咖啡部分:

UPDATE:: I forget all about the Coffee part:

root = exports ? this
root.test2 = (->
  doSomething = -> alert this.state
  return {
    state: undefined
    doSomething: doSomething
  }
)()

或者:

root = exports ? this
root.test2 = {
    state: undefined
    doSomething: -> alert this.state
}

这篇关于在Javascript / Coffeescript中如何在测试中模拟出模块的公共变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆