{"id":32793,"date":"2021-06-10T16:09:22","date_gmt":"2021-06-10T20:09:22","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=32793"},"modified":"2022-05-05T13:49:06","modified_gmt":"2022-05-05T17:49:06","slug":"how-to-test-swift-async-await-code-with-xctest","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2021\/06\/10\/how-to-test-swift-async-await-code-with-xctest\/","title":{"rendered":"How to Test Swift async\/await Code With XCTest"},"content":{"rendered":"<p><a href=\"https:\/\/mokacoding.com\/blog\/how-to-test-async-await-code-in-swift\/\">Giovanni Lodi<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mokacoding.com\/blog\/how-to-test-async-await-code-in-swift\/\">\n<p>Because async\/await is a feature at the Swift language level, to test an <code>async<\/code> function we can use the same approach we&rsquo;d use to consume that code in production: call it with <code>await<\/code>.<\/p>\n<p>[&#8230;]<\/p>\n<p>To appreciate how neater this is than the approach we used before the introduction of <code>async await<\/code>, let me show you the same test but for a <code>chopVegetables<\/code> version using callbacks and <code>Result<\/code>.<\/p>\n<p>[&#8230;]<\/p>\n<p>Unfortunately the <code>XCTAssertThrowsError<\/code> and the other assertion APIs don&rsquo;t support concurrency yet, so <code>do catch<\/code> is the only option to test the error path of <code>async<\/code> code.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Giovanni Lodi: Because async\/await is a feature at the Swift language level, to test an async function we can use the same approach we&rsquo;d use to consume that code in production: call it with await. [&#8230;] To appreciate how neater this is than the approach we used before the introduction of async await, let me [&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":"2021-06-10T20:09:25Z","apple_news_api_id":"428652e1-1406-4f25-a905-f14535cb5c33","apple_news_api_modified_at":"2022-05-05T17:49:09Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/AQoZS4RQGTyWpBfFFNctcMw","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":[800,31,2078,30,2077,71,2200,901,268,226],"class_list":["post-32793","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-concurrency","tag-ios","tag-ios-15","tag-mac","tag-macos-12","tag-programming","tag-swift-concurrency","tag-swift-programming-language","tag-testing","tag-xcode"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32793","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=32793"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32793\/revisions"}],"predecessor-version":[{"id":32794,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32793\/revisions\/32794"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=32793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=32793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=32793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}