javascript 如果`beforeEach` 中的代码是异步的,如何在 Jasmine 中测试方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10527394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test a method in Jasmine if the code in `beforeEach` is asynchronous?
提问by Freewind
I'm trying to write some tests with Jasmine, but now have a problem if there are some code is asynchronous in beforeEach
.
我正在尝试使用 Jasmine 编写一些测试,但是如果beforeEach
.
The sample code looks like:
示例代码如下所示:
describe("Jasmine", function() {
var data ;
beforeEach(function(){
console.log('Before each');
getSomeDataFromRemote(function(res){
data = res;
});
});
it("test1", function() {
expect(data).toBe(something);
console.log('Test finished');
});
});
You can see, in the beforeEach
, I want to get some data from remote, and assign it to the data
asynchronously.
你可以看到,在 中beforeEach
,我想从远程获取一些数据,并data
异步分配给它。
But in the test1
, when I try to verify:
但是在test1
, 当我尝试验证时:
expect(data).toBe(something);
The data is undefined
, because getSomeDataFromRemote
has not finished yet.
数据是undefined
,因为getSomeDataFromRemote
还没有完成。
How to fix it?
如何解决?
回答by Aaron Powell
Just like the async stuff within an it
you can use the runs
and waitsFor
in your beforeEach:
就像内的异步的东西it
,你可以使用runs
,并waitsFor
在你的beforeEach:
define( 'Jasmine' , function () {
var data ;
beforeEach(function(){
runs( function () {
getSomeDataFromRemote(function(res){
data = res;
});
});
waitsFor(function () { return !!data; } , 'Timed out', 1000);
});
it("test1", function() {
runs( function () {
expect(data).toBe(something);
});
});
});
Although I'm going to assume that it's because this was test code I think you should probably have the getSomeDataFromRemote
call inside your it
as that's actually what you're testing ;)
尽管我将假设这是因为这是测试代码,但我认为您可能应该在getSomeDataFromRemote
内部调用该调用,it
因为这实际上是您正在测试的内容;)
You can see some larger examples in some tests I've written for an async API here: https://github.com/aaronpowell/db.js/blob/f8a1c331a20e14e286e3f21ff8cea8c2e3e57be6/tests/public/specs/open-db.js
你可以在我为异步 API 编写的一些测试中看到一些更大的例子:https: //github.com/aaronpowell/db.js/blob/f8a1c331a20e14e286e3f21ff8cea8c2e3e57be6/tests/public/specs/open-db.js
回答by Timbergus
Be careful because in the new Jasmine 2.0 this is going to change and it will be mochastyle. You have to use done()
function in beforeEach()
and it()
. For example, imagine you want to test if a page exists and is not empty, in a LAMP server, using jQuery $.get
. First you need to add jQuery to the SpecRunner.html
file, and in your spec.js
file:
小心,因为在新的 Jasmine 2.0 中,这将发生变化,它将成为mocha风格。您必须done()
在beforeEach()
and 中使用函数it()
。例如,假设您想在 LAMP 服务器中使用 jQuery 测试页面是否存在且不为空$.get
。首先,您需要将 jQuery 添加到SpecRunner.html
文件中,并在您的spec.js
文件中:
describe('The "index.php" should', function() {
var pageStatus;
var contents;
beforeEach(function (done) {
$.get('views/index.php', function (data, status) {
contents = data;
pageStatus = status;
done();
}).fail(function (object, status) {
pageStatus = status;
done();
});
});
it('exist', function(done) {
expect(status).toBe('success');
done();
});
it('have content', function(done) {
expect(contents).not.toBe('');
expect(contents).not.toBe(undefined);
done();
});
});
As you can see, you pass the function done()
as a parameter for beforeEach()
and it()
. When you run the test, it()
won't be launched until done()
has been called in beforeEach()
function, so you are not going to launch the expectations until you have the response from the server.
如您所见,您将函数done()
作为beforeEach()
and的参数传递it()
。当您运行测试时,在函数中调用it()
之前不会启动,因此在您收到服务器的响应之前,您不会启动期望。done()
beforeEach()
The page exists
该页面存在
If the page exists we capture the status and the data from the response of the server, and we call done()
. Then we check if the status is "success" and if the data is not empty or undefined.
如果页面存在,我们从服务器的响应中捕获状态和数据,然后调用done()
. 然后我们检查状态是否为“成功”以及数据是否不为空或未定义。
The page does not exist
该页面不存在
If the page does not exist we capture the status from the response of the server, and we call done()
. Then we check if the status is not "success" and if the data is empty or undefined (that must be because the file does not exist).
如果页面不存在,我们从服务器的响应中捕获状态,然后调用done()
. 然后我们检查状态是否不是“成功”以及数据是否为空或未定义(那一定是因为文件不存在)。
回答by x1a4
回答by Lars
I have an in-depth article describing how to test asynchronous javascript functions using Jasmine: http://www.larsavery.com/blog/how-to-test-asynchronous-javascript-functions-using-jasmine/
我有一篇深入的文章描述了如何使用 Jasmine 测试异步 javascript 函数:http: //www.larsavery.com/blog/how-to-test-asynchronous-javascript-functions-using-jasmine/