简体   繁体   English

如何测试N秒后是否未调用方法?

[英]How to test if a method isn't called after N seconds?

I'm writing some tests and I need to check that "nothing happens" when the app receive a request, that request has to be completely ignored. 我正在编写一些测试,我需要检查当应用程序收到请求时“什么也没有发生”,该请求必须完全忽略。 The way to know if the request was ignored or not is to check two delegates that have to receive nothing after the request is sent. 知道是否忽略请求的方法是检查发送请求后必须什么都不接收的两个委托。

I have no idea how to test this: 我不知道该如何测试:

func testIgnore() {

    sut.message() {
        //this doesn't have to be called if test succeed
    }

    sut.appointment() {
        //this doesn't have to be called if test succeed
    }

    stub(isHost("test.com")) { (request: NSURLRequest) -> OHHTTPStubsResponse in
        let data = "specific data".dataUsingEncoding(NSUTF8StringEncoding)
        return OHHTTPStubsResponse(data: data!, statusCode: 200, headers: nil)
    }

}

Any idea?? 任何想法?? I'm completely lost, because I can't use expectations because if the expectation timeouts that's a failed test and this scenario that would be a successful test... 我完全迷失了,因为我不能使用期望,因为如果期望超时是失败的测试,而这种情况将是成功的测试...

I need to wait for example 3 seconds, if the blocks are not called then that's a success 我需要等待3秒钟,如果未调用块,则表示成功

Add XCTFail() in the two methods you expect not to be called so your test will fail if they do get called. 在您不希望被调用的两个方法中添加XCTFail() ,因此如果调用它们,测试将失败。

Then simply use XCTest methods for asynchronous testing to wait for 3 seconds, eg: 然后只需使用XCTest方法进行异步测试即可等待3秒钟,例如:

let exp = self.expectationWithDescription("3 seconds passed")
dispatch_after(…3 seconds…, dispatch_get_main_queue()) {
  exp.fulfill()
}
self.waitForExpectationsWithTimeout(4)

⚠️ This is pseudo-code as I'm on my iPhone so it's hard to write long code there, and I may have done some typos in the methods names. ⚠️这是伪代码,因为我在iPhone上,所以在那里很难写长代码,而且我可能在方法名称中做了一些错别字。 Adjust as needed. 根据需要进行调整。


PS: I don't see how this question is related to OHHTTPStubs , btw. PS:我不知道这个问题与OHHTTPStubs ,顺便说一句。 All your question is about is "how to test if a method isn't called after N seconds"… 您所有的问题是“如何测试N秒后是否未调用方法”…

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM