如何解决如何在reactJs中进行settimeout的单元测试
如何使用玩笑/酶在反应中测试setTimeout函数
using (var db = new FMBDBSTAGEEntities())
{
var dataIterator = db.FMB_Reports_Action.Where(x => x.Active);
foreach (var item in dataIterator)
{
if (item.Active)
{
string filePath = @"C:\Users\fmb03113.FMB\Documents\testing";
string libraryName = item.DropOffPath;
string siteUrl = "https://reports.fmb.com/";
string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
MovingFiles(filePath,libraryName,siteUrl,fileName);
}
}
}
}
public static void MovingFiles(string filePath,string libraryName,string siteUrl,string fileName)
{
using (ClientContext ctx = new ClientContext(siteUrl))
{
FileCreationInformation fcInfo = new FileCreationInformation();
fcInfo.Url = fileName;
fcInfo.Overwrite = true;
fcInfo.Content = System.IO.File.ReadAllBytes(filePath);
//fcInfo.Content = System.IO.File.ReadAllBytes(filePath);
Web myWeb = ctx.Web;
List myLibrary = myWeb.Lists.GetByTitle(libraryName);
var fileToMOve = myLibrary.RootFolder.Files.Add(fcInfo);
//fileToMOve.MoveTo(libraryName,movce);
//myLibrary.RootFolder.MoveTo(libraryName);
ctx.ExecuteQuery();
}
}
}
我尝试过这个:
setTimeout(() => {
},500);
但是测试失败
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenLastCalledWith(() => { },500);
有人可以帮我吗?
解决方法
您可以使用此:
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function),500);
,
您可以使用jest.useFakeTimers()
启用假计时器来测试setTimeout
来实现所需的测试用例。
jest.useFakeTimers();
test('test setTimeout',() => {
const fn = jest.fn();
setTimeout(fn,500);
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenLastCalledWith(fn,500);
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。