{"id":61,"date":"2022-01-15T12:25:02","date_gmt":"2022-01-15T12:25:02","guid":{"rendered":"https:\/\/baldsolutions.com\/?p=61"},"modified":"2022-01-16T16:34:03","modified_gmt":"2022-01-16T16:34:03","slug":"how-to-unit-test-method-invocation-that-accepts-builder-style-parameter-with-nsubstitute","status":"publish","type":"post","link":"https:\/\/baldsolutions.com\/index.php\/2022\/01\/15\/how-to-unit-test-method-invocation-that-accepts-builder-style-parameter-with-nsubstitute\/","title":{"rendered":"Unit test for a method that accepts builder-style parameter with NSubstitute"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Introduction<\/h1>\n\n\n\n<p>Sometimes it happens that you should write unit tests&#8230; <\/p>\n\n\n\n<p>Just kidding &#8211; you HAVE TO write unit tests (me as well).<\/p>\n\n\n\n<p>In a perfect world the method we want to write a unit-test for is well-written, concise and returns concrete value. Unfortunately, there are methods that seems to be intricate in terms of unit tests, at least at first glance. Let&#8217;s consider the following scenario:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class CustomClass\n{\n    private readonly ICustomService _customService;\n\n    public CustomClass(ICustomService customService)\n    {\n        _customService = customService;\n    }\n\n    public void RunProcess(int param_1, int param_2, int param_3)\n    {\n        _customService.Run(\n            descriptorBuilder =&gt; descriptorBuilder\n                .BuildMyProperty_1(param_1)\n                .BuildMyProperty_2(param_2)\n                .BuildMyProperty_3(param_3)\n        );\n    }\n}\n\npublic interface ICustomService\n{\n    void Run(Func&lt;IDescriptor, IDescriptor&gt; descriptorBuilder);\n}\n\npublic interface IDescriptor\n{\n    public IDescriptor BuildMyProperty_1(int myProperty_1);\n    public IDescriptor BuildMyProperty_2(int myProperty_2);\n    public IDescriptor BuildMyProperty_3(int myProperty_3);\n}<\/code><\/pre>\n\n\n\n<p>Having class like this we want to write unit test for the <code>RunProcess<\/code> method. The method accepts three int parameters. Eventually the parameters are passed to the <code>ICustomService.Run<\/code> method that accepts builder-style parameter. Moreover, the  <code>RunProcess<\/code> returns nothing. <\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Unit test using NSubstitute<\/h1>\n\n\n\n<p><a title=\"https:\/\/nsubstitute.github.io\/\" href=\"https:\/\/nsubstitute.github.io\/\">NSubstitute<\/a> might be your best friend when it comes to mock objects and assert received calls. The syntax is succinct and intuitive so I would suggest you to consider to experiment with the library. <\/p>\n\n\n\n<p>Let&#8217;s build unit test in a AAA manner &#8211; arrange, act, assert &#8211; which NSubstitute is designed for.<\/p>\n\n\n\n<p>The test name is <em>RunProcess_CustomServiceExecutedWithPassedParameters_Success<\/em>. <br>The name follows the pattern: &lt;MethodNameThatIsBeingTested&gt;_&lt;ExpectedInternalMethodBehaviour&gt;_&lt;Status&gt;.<br>I guess there are as many test naming conventions as engineers that create it. Furthermore, I believe that as long as the convention informs you well about test internals it is good enough.<\/p>\n\n\n\n<p> The test looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>&#91;Fact]\npublic void RunProcess_CustomServiceExecutedWithPassedParameters_Success()\n{\n    \/\/arrange\n    var callParameters = (111, 22, 3333);\n\n    var mockDescriptor = Substitute.For&lt;IDescriptor&gt;();\n    mockDescriptor.ReturnsForAll&lt;IDescriptor&gt;(mockDescriptor); \n\n    var customService = Substitute.For&lt;ICustomService&gt;();\n    CustomClass customClass = new CustomClass(customService);\n\n    \/\/act\n    customClass.RunProcess(\n        param_1: callParameters.Item1,\n        param_2: callParameters.Item2,\n        param_3: callParameters.Item3\n    );\n\n    \/\/assert\n    customService\n        .Received(1)\n        .Run(Arg.Is&lt;Func&lt;IDescriptor, IDescriptor&gt;&gt;(\n                descriptorBuilder =&gt; AssertDescriptorFunc(\n                    descriptorBuilder,\n                    mockDescriptor,\n                    callParameters\n                )\n            )\n        );\n}\n\nprivate bool AssertDescriptorFunc(\n    Func&lt;IDescriptor, IDescriptor&gt; descriptorFunc,\n    IDescriptor mockDescriptor,\n    (int, int, int) callParameters\n)\n{\n    descriptorFunc(mockDescriptor);\n\n    mockDescriptor.Received(1).BuildMyProperty_1(callParameters.Item1);\n    mockDescriptor.Received(1).BuildMyProperty_2(callParameters.Item2);\n    mockDescriptor.Received(1).BuildMyProperty_3(callParameters.Item3);\n\n    return true;\n}<\/code><\/pre>\n\n\n\n<p>Let&#8217;s briefly summarize what has happened up there. The test asserts that after the <code>customClass.RunProcess<\/code> invocation the <code>customService.Run<\/code> method received exactly one call. The invocation happened with exactly one parameter. The parameter was type of <code>Func&lt;IDescriptor,IDescriptor&gt;<\/code> and then was asserted using <code>AssertDescriptorFunc<\/code> method.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Summary<\/h1>\n\n\n\n<p>To sum up, thanks to the NSubstitute library we are able to write unit test even for such an unusual scenario. NSubstitute offers much more than I&#8217;ve just presented here. I highly recommend to check the  NSubstitute documentation and spend some time on playing with its functionalities.<\/p>\n\n\n\n<p>If you want to give it a try you can find attached scenario in my <a href=\"https:\/\/github.com\/tglowka\/baldsolutions\/tree\/master\/.NET\" title=\"https:\/\/github.com\/tglowka\/baldsolutions\/tree\/master\/.NET\" target=\"_blank\" rel=\"noreferrer noopener\">github<\/a>, project: nsubstitute_1, test project: nsubstitute_1-test.<\/p>\n\n\n\n<p> Have a nice day, bye!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Sometimes it happens that you should write unit tests&#8230; Just kidding &#8211; you HAVE TO write unit tests (me as well). In a perfect&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/baldsolutions.com\/index.php\/2022\/01\/15\/how-to-unit-test-method-invocation-that-accepts-builder-style-parameter-with-nsubstitute\/\">Continue reading<span class=\"screen-reader-text\">Unit test for a method that accepts builder-style parameter with NSubstitute<\/span><\/a><\/div>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-61","post","type-post","status-publish","format-standard","hentry","category-net","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/61","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/comments?post=61"}],"version-history":[{"count":10,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/61\/revisions"}],"predecessor-version":[{"id":74,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/61\/revisions\/74"}],"wp:attachment":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/media?parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/categories?post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/tags?post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}