{"id":9171,"date":"2014-07-17T15:08:20","date_gmt":"2014-07-17T19:08:20","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=9171"},"modified":"2014-07-22T15:02:55","modified_gmt":"2014-07-22T19:02:55","slug":"asynchronous-unit-testing-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/07\/17\/asynchronous-unit-testing-in-swift\/","title":{"rendered":"Asynchronous Unit Testing in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/www.subfurther.com\/blog\/2014\/07\/16\/asynchronous-unit-testing-in-swift-the-good-ugly-the-bad-and-the\/\">Chris Adamson<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.subfurther.com\/blog\/2014\/07\/16\/asynchronous-unit-testing-in-swift-the-good-ugly-the-bad-and-the\/\"><p>Fortunately, this is where Xcode 6&#8242;s asynchronous testing comes in. It allows us to create <code>XCTestExpectation<\/code> objects, which are not tests but timers. We create expectations with <code>XCTestCase<\/code>&rsquo;s <code>expectationWithDescription()<\/code>, which just takes a string to describe what we&rsquo;re waiting for. Then, prior to the end of the <code>test&#8230;<\/code> method, we call <code>waitForExpectationsWithTimeout()<\/code>, passing in a timeout period and a completion handler closure. This prevents the test method from exiting until either the timeout expires, or some asynchronous test code calls <code>fulfill()<\/code> on the expectation object, which unblocks it.<\/p><\/blockquote>\n<p>Update (2014-07-22): <a href=\"http:\/\/nshipster.com\/xctestcase\/\">Mattt Thompson<\/a>:<\/p>\n<blockquote cite=\"http:\/\/nshipster.com\/xctestcase\/\"><p>Perhaps the most exciting feature added in Xcode 6 is built-in support for asynchronous testing, with the <code>XCTestExpectation<\/code> class. Now, tests can wait for a specified length of time for certain conditions to be satisfied, without resorting to complicated GCD incantations.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Chris Adamson: Fortunately, this is where Xcode 6&#8242;s asynchronous testing comes in. It allows us to create XCTestExpectation objects, which are not tests but timers. We create expectations with XCTestCase&rsquo;s expectationWithDescription(), which just takes a string to describe what we&rsquo;re waiting for. Then, prior to the end of the test&#8230; method, we call waitForExpectationsWithTimeout(), passing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,71,901,268],"class_list":["post-9171","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-programming","tag-swift-programming-language","tag-testing"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=9171"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9171\/revisions"}],"predecessor-version":[{"id":9208,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9171\/revisions\/9208"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=9171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=9171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=9171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}