[英]Jasmine testing if element fades out
I have the following two functions that I'm writing some Jasmine tests for: 我有以下两个函数供我编写一些Jasmine测试:
var showSpinner = function () {
$('#spinner').remove();
$('<div id="spinner"><i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i></div>')
.appendTo('body')
.hide()
.fadeIn();
};
var hideSpinner = function () {
$('#spinner').fadeOut(function () {
$(this).remove();
});
};
and my tests are as follows: 我的测试如下:
it('show spinner', function () {
showSpinner();
expect($('#spinner').length).toEqual(1);
});
it('hide spinner', function () {
hideSpinner();
expect($('#spinner').length).toEqual(0);
});
The first test works fine. 第一次测试工作正常。 However due to the fadeOut
on the hideSpinner
the second test fails. 但是由于fadeOut
的hideSpinner
第二次测试失败。
I tried to amend it to use a timeout eg 我试图修改它以使用超时,例如
it('hide spinner', function () {
hideSpinner();
setTimeout(function () {
expect($('#spinner').length).toEqual(0);
}, 100);
});
But that causes the test to pass but with a warning about having no expectations... 但这会导致测试通过,但会发出警告,提醒您不要期望...
How can I test this? 我该如何测试?
You should read about Jasmine's Asynchronous Support : 您应该阅读有关Jasmine的异步支持的信息 :
it('hide spinner', function(done) {
hideSpinner();
setTimeout(function() {
expect($('#spinner').length).toEqual(0);
done();
}, 400);
});
400
ms is the default timeout for $.fadeOut
. $.fadeOut
的默认超时为400
毫秒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.