`

Javascript 测试框架之 隐式声明 之 describe

阅读更多
为什么使用 javascript 测试框架时,没有显式导入 describe,却可以直接使用:?

https://stackoverflow.com/questions/12209582/the-describe-keyword-in-javascript

问题:
So I am a newbie in javascript and i had been going through some one else's code and I found this..
我是 javascript  的新手,我遇到了别人写的代码如下:

describe('deviceready', function() {
    it('should report that it fired', function() {
        spyOn(app, 'report');
        app.deviceready();
        expect(app.report).toHaveBeenCalledWith('deviceready');
    });
});



What I don't understand is: What exactly does the describe keyword do?
我不明白的是:describe 关键字到底做了什么?


回答:
Describe is a function in the Jasmine testing framework. It simply describes the suite of test cases enumerated by the "it" functions.
Describe 是 Jasmine 测试框架的一个函数。它只是描述了一组用 it 函数枚举的测试用例。

Also used in the mochajs framework.
Describe 也被用在了 mochajs 测试框架中。



Describe is not part of Javascript, it is a function defined in the library you used (namely Jasmine)
Describe 不属于原生 Javascript 的一部分,它是定义在 Jasmine 框架的类库中。

http://jasmine.github.io/


--------------------------------------
引用

"A test suite begins with a call to the global Jasmine function describe with two parameters: a string and a function. The string is a name or title for a spec suite - usually what is being tested. The function is a block of code that implements the suite."

来源:
http://jasmine.github.io/2.0/introduction.html



-------------------------------------------------------


https://www.bignerdranch.com/blog/why-do-javascript-test-frameworks-use-describe-and-beforeeach/


-------------------------------------------------------
原理:

Nodejs Global Functions

https://stackoverflow.com/questions/29544417/nodejs-global-functions

我有一个非常基础的函数,在 functions.js 文件中:
// functions.js
function echo(input){
    process.stdout.write(echo);
}


我在另一个文件中使用它:
// another_file.js
echo("hello!");



然后在 mainFile 文件中运行:

// main_file.js
require("functions.js");
require("another_file.js");





结果出错了:

ReferenceError: echo is not defined



Is there a way for me to make a function that is global like that without havingin to use exports?

有没有一个方法,可以不使用 exports 关键字,而使这个函数可以全局访问?


回答:

Inside functions.js you'll have access to node's global variable, which is like the window variable in a browser. As Plato suggested in a comment, you can add this to the global by simply doing echo = function echo(input){ ... }. However, this will throw an error if you're using strict mode, which is intended to catch common mistakes (like accidentally creating global variables).

在 functions.js 文件中,你可以访问 nodejs 的全局变量 global,
这跟浏览器里面的 window 变量差不多。

像 Plato 建议的是,你可以将它添加到 gobal 中,通过以下方法:


echo = function echo(input){ 
   process.stdout.write(echo);
};

// 注意:echo 变量前,没有任何修饰符。


但是,如果在 strict 模式下,这会报错。

-------------------------------------------------

One way to safely add echo as a global is to add it to the global global variable.
一个安全的方法是把 echo 添加到 global 变量中:


"use strict";

global.echo = function echo(input) {
    process.stdout.write(input);
}





https://stackoverflow.com/questions/29544417/nodejs-global-functions



























-
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics