{"id":8347,"date":"2014-01-21T17:58:33","date_gmt":"2014-01-21T22:58:33","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=8347"},"modified":"2014-01-21T17:58:33","modified_gmt":"2014-01-21T22:58:33","slug":"ask-for-the-clock","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/01\/21\/ask-for-the-clock\/","title":{"rendered":"Ask for the Clock"},"content":{"rendered":"<p><a href=\"http:\/\/twistedoakstudios.com\/blog\/Post3516_rule-of-thumb-ask-for-the-clock\">Craig Gidney<\/a> (via <a href=\"http:\/\/www.reddit.com\/r\/programming\/comments\/1sg70r\/tdd_unit_tests_and_the_passage_of_time\/\">Reddit<\/a>):<\/p>\n<blockquote cite=\"http:\/\/twistedoakstudios.com\/blog\/Post3516_rule-of-thumb-ask-for-the-clock\"><p>The method I use to create testable time-dependent code is to ask for a &ldquo;clock&rdquo;. A clock is a provider for the progression of time, an instance of an interface with methods for all the various time-related things the code might want to do: delaying an action, measuring elapsed time, creating periodic events, querying the date, whatever you need.<\/p>\n<p>When the code is being run in production, the provided clock will be advancing with respect to external time. When the code is being tested, the provided clock will be a &ldquo;fake&rdquo; manually controlled instanced that the test can advance as needed.<\/p>\n<p>[&#8230;]<\/p>\n<p>Interestingly, once you&rsquo;re asking for clocks everywhere, you can start reaping some benefits unrelated to testing.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Craig Gidney (via Reddit): The method I use to create testable time-dependent code is to ask for a &ldquo;clock&rdquo;. A clock is a provider for the progression of time, an instance of an interface with methods for all the various time-related things the code might want to do: delaying an action, measuring elapsed time, creating [&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":[71,268],"class_list":["post-8347","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-programming","tag-testing"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8347","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=8347"}],"version-history":[{"count":0,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8347\/revisions"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=8347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=8347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=8347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}