{"id":3461,"date":"2015-03-25T13:00:59","date_gmt":"2015-03-25T13:00:59","guid":{"rendered":"http:\/\/writeasync.net\/?p=3461"},"modified":"2015-03-25T02:57:07","modified_gmt":"2015-03-25T02:57:07","slug":"object-mother-and-tasks","status":"publish","type":"post","link":"http:\/\/writeasync.net\/?p=3461","title":{"rendered":"Object Mother and Tasks"},"content":{"rendered":"<p>In a previous post &#8220;<a href=\"http:\/\/writeasync.net\/?p=2721\">Fluent async testing<\/a>,&#8221; I introduced a <a href=\"https:\/\/github.com\/brian-dot-net\/writeasync\/tree\/master\/projects\/FluentSample\">sample library<\/a> to beautify unit tests relying on Task-based assertions. Tests for this type of library typically make heavy use of &#8220;canned Tasks&#8221; to ensure every case is handled correctly. There are four common cases representing the most often observable states of Task:<\/p>\n<ul>\n<li>Pending completion<\/li>\n<li>Completed successfully<\/li>\n<li>Completed with exception(s)<\/li>\n<li>Canceled<\/li>\n<\/ul>\n<p>(Technically there are two variants of each state above, one for <code>Task<\/code> with no result and one for <code>Task&lt;T&gt;<\/code>.)<\/p>\n<p>Writing boilerplate code to manufacture all these Tasks in every test method becomes tedious pretty fast, so I almost always extract each one into a central method I can reuse. For FluentSample, I put the code in <a href=\"https:\/\/github.com\/brian-dot-net\/writeasync\/blob\/master\/projects\/FluentSample\/test\/FluentSample.Test.Unit\/TaskResultBuilder.cs\">TaskResultBuilder<\/a>. The pattern used here actually has a name: <a href=\"http:\/\/martinfowler.com\/bliki\/ObjectMother.html\">&#8220;Object Mother&#8221;<\/a>.<\/p>\n<p>In .NET 4.5, the &#8220;object grandmother&#8221; for Task is <a href=\"http:\/\/blogs.msdn.com\/b\/pfxteam\/archive\/2009\/06\/02\/9685804.aspx\"><code>TaskCompletionSource&lt;T&gt;<\/code><\/a>. For example, this is how you can get a Task representing an exception:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nTaskCompletionSource&lt;bool&gt; tcs = new TaskCompletionSource&lt;bool&gt;();\r\ntcs.SetException(new InvalidOperationException(&quot;This is expected!&quot;));\r\nreturn tcs.Task;\r\n<\/pre>\n<p>Incidentally, <a href=\"http:\/\/blogs.msdn.com\/b\/pfxteam\/archive\/2015\/02\/02\/new-task-apis-in-net-4-6.aspx\">.NET 4.6 reduces the boilerplate<\/a> even further with <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.threading.tasks.task.fromexception(v=vs.110).aspx\"><code>Task.FromException<\/code><\/a> and <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.threading.tasks.task.fromcanceled(v=vs.110).aspx\"><code>Task.FromCanceled<\/code><\/a>.<\/p>\n<p>Thanks, <a href=\"http:\/\/blogs.msdn.com\/b\/pfxteam\/\"><em>pfxteam<\/em><\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post &#8220;Fluent async testing,&#8221; I introduced a sample library to beautify unit tests relying on Task-based assertions. Tests for this type of library typically make heavy use of &#8220;canned Tasks&#8221; to ensure every case is handled correctly.&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,41],"tags":[],"class_list":["post-3461","post","type-post","status-publish","format-standard","hentry","category-async","category-tdd"],"_links":{"self":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/3461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3461"}],"version-history":[{"count":0,"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/3461\/revisions"}],"wp:attachment":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3461"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}