- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个在后台运行的函数,完成后它会在主线程中更新 UI。我注意到当代码到达对主线程的调用时单元测试失败。我该如何纠正这个问题?
例如注意:long 描述了项目中的伪逻辑,不是确切的代码
在主代码中:
func getResponse(identifier : String, completion :(success :Bool)->){
// uses identifier to request data via api and on completion:
completion(status: true)
}
testObject.getResponse(wantedValue){(success) in
if status == true {
dispatch_async(dispatch_get_main_queue()){
self.presentViewController(alertController, animated: true, completion: nil)
}
}
}
在单元测试中
func testGetResponse(){
var testObject = TestObject()
var expectation = self.self.expectationWithDescription("Response recieved")
testObject.getResponse(wantedValue){(success) in
expectation.fulfill()
}
self.waitForExpectationsWithTimeout(10) { (error) in
XCTAssertTrue(testViewController.presentedViewController as? CustomViewController)
}
}
这似乎是一个潜在的僵局,但我不确定如何解决它。
最佳答案
waitForExpectationsWithTimeout 也是您的异步函数未被调用或未正确完成(因此未调用 fulfill() 方法)情况下的回退方法。
尝试检查错误对象。
我建议在执行 fullfill() 调用之前进行验证。
请参阅以下 Swift 3 示例代码,了解如何使用 fullfill 和 waitForExpectationsWithTimeout。
func testGetResponse(){
var testObject = TestObject()
var validationExpectation = expectation(description: "Response received")
testObject.getResponse(wantedValue){(success) in
// Do your validation
validationExpectation.fulfill()
// Test succeeded
}
waitForExpectationsWithTimeout(60) { (error) in
if let error = error {
// Test failed
XCTFail("Error: \(error.localizedDescription)")
}
}
}
关于ios - 在单元测试中处理 MainThread 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44931458/
我很好奇为什么下面是内存泄漏,因为 mHandler 是在 mainThread 上创建的,现在当调用 onDestroy 时它不会杀死线程吗? Activity 被销毁后,处理程序如何存在?我还没有
我有一个方法: /** * Call event without a value */ @MainThread public void call() { setValue(null); }
我有一个在后台运行的函数,完成后它会在主线程中更新 UI。我注意到当代码到达对主线程的调用时单元测试失败。我该如何纠正这个问题? 例如注意:long 描述了项目中的伪逻辑,不是确切的代码 在主代码中:
我正在考虑如何将结果数据从某个工作线程传回客户端而不进行复制。 Worker 住在其他线程中,而 BigData 继承了 QObject。我的想法是改变数据的所有权: class Worker: pu
当大多数用例都用作后台线程时,viewModelScope 默认为 MainThread 的原因是什么? 在我的项目中,我仍然想不出一个需要在 ViewModel 中使用主线程的示例。 另外,有没有比
在通过代码呈现新的 UIViewController 之后,在每个地方(在 viewModel 或 viewController 中)使用 .Drive,我得到这个错误:drive* 系列方法只能从
我正在尝试将图像放入 cell.image 中。如果图像已经加载(因此,在缓存中),我会这样做。否则,我会使用 sendAsynchronousRequest 在后台下载它。 这是我的代码: var
我有这个方法: func stepThree() { operation = "prDatas" let entries = self.data.componentsSeparated
我有一个带有名为 MainForm 的窗体的 winform。我有一个名为 ObjMgr 的静态类。在 ObjMg 中,我还有其他几个静态类、值,但现在不重要。 在 ObjMgr 类中,我还有一个 s
好的,我们实现了一个 MVP 层,很好地展示并使用 RxJava 和 RxAndroid。我们拍了this作为例子并以此为基础。 当 Presenter 被调用开始执行时,它会向 Model Inte
我有 UITableView,其中包含一些内容,这些内容以异步 方式加载。当用户旋转设备时,我需要在 -willRotateToInterfaceOrientation 方法中制作 [tableVie
这个问题在这里已经有了答案: How can I fix 'android.os.NetworkOnMainThreadException'? (66 个答案) NetWork On main th
我有一个运行在 Linux 服务器上的多线程 Python 应用程序。我可以使用 PyDev 的 Debug Server 远程调试进去,这似乎是一个非常有值(value)的调试资源。然而,我发现有一
如何在后台线程中执行此代码? .subscribeOn(Schedulers.computation()) onNext() { touchEverythingInTheUiThreadFr
我找到了大量关于如何在 UI 线程上调用函数的资源,但我有一些逻辑只允许从主线程运行。有没有办法在主线程上获取调度程序并对其进行调用? 最佳答案 “调度程序”是特定于特定 UI 框架(此处:WPF)的
我尝试以相同的 X 和 Y 步顺序将 JLabel 从位置 A 移动到位置 B。该方法工作正常,但没有更新我的 gui。我在下面向您展示我的代码: 当我不调用 .join() 方法时,代码可以工作,但
我正在尝试使用 Volley 库获取数据以在 Andriod 应用程序中形成服务器。当我请求获取数据时我的数据太大但问题是 Volley 超时错误我也使用了RetryPolicy但问题是一样的。我该如
我有一个 while 语句在后台运行。 - (IBAction)startButton { [self performSelectorInBackground:@selector(Counter)
RxJava 中是否有一个 Scheduler api 与 RxAndroid 中的 AndroidSchedulers.mainThread() 同义。因此,如果我在新线程上安排任务并想在 Java
我正在查看一些旧代码,并看到以下 if 语句: if ([NSThread currentThread] != [NSThread mainThread]) 我偷偷怀疑这和 if (![[NSThre
我是一名优秀的程序员,十分优秀!