{"id":1041,"date":"2017-02-16T22:01:53","date_gmt":"2017-02-16T12:01:53","guid":{"rendered":"http:\/\/www.moneystock.net\/wp_e\/?p=1041"},"modified":"2017-02-16T22:03:48","modified_gmt":"2017-02-16T12:03:48","slug":"recursive-mocking-using-moq","status":"publish","type":"post","link":"https:\/\/moneystock.net\/wp_e\/2017\/02\/16\/recursive-mocking-using-moq\/","title":{"rendered":"Recursive mocking using Moq"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>When we want to mock a method that&#8217;s inside an interface which is inside another interface. This can go tricky.<\/p>\n<p>I&#8217;ll have a look at easy way mocking recursively using Moq.<\/p>\n<h3>Prerequisite<\/h3>\n<p>Moq mocking library (\u00a0<a href=\"https:\/\/www.nuget.org\/packages\/Moq\/\">Nuget package<\/a>)<\/p>\n<p><a href=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2017\/02\/moq2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1043\" src=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2017\/02\/moq2.jpg\" alt=\"\" width=\"994\" height=\"251\" srcset=\"https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2017\/02\/moq2.jpg 994w, https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2017\/02\/moq2-300x76.jpg 300w, https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2017\/02\/moq2-768x194.jpg 768w\" sizes=\"auto, (max-width: 994px) 100vw, 994px\" \/><\/a><\/p>\n<h3>Case<\/h3>\n<p>There is a class having a dependency of IFoo interface, and IFoo contains IBar interface as a property and IBar\u00a0have a method, which is what I want to mock.<\/p>\n<p>Let&#8217;s think about a class like below.<\/p>\n<pre>\u00a0public class MoqTest\r\n {\r\n private IFoo _foo;\r\n\r\n public MoqTest(IFoo foo)\r\n {\r\n   this._foo = foo;\r\n }\r\n\r\n public string Run(string id, string pass)\r\n {\r\n   var result = _foo.Bar.DoBar(id, pass);\r\n\r\n   \/\/ do something with result\r\n\r\n   return result;\r\n }\r\n\r\n public string RunSimple()\r\n {\r\n   var result = _foo.DoFoo();\r\n  \r\n   \/\/ do something with result\r\n\r\n   return result;\r\n }\r\n\r\n }<\/pre>\n<p>MoqTest class have a dependency of IFoo. IFoo has a Bar class and Bar class have a DoBar method. We want to mock Foo, Bar, and Dobar.<\/p>\n<p>What I need to test is the logic inside Run method of MoqTest class, so I need to mock Foo, Bar and Dobar.<\/p>\n<p>Now, quickly have a look at IFoo and IBar<\/p>\n<pre>public interface IFoo\r\n{\r\n  IBar Bar { get; }\r\n  string DoFoo();\r\n}\r\n\r\npublic interface IBar\r\n{\r\n  string DoBar(string id, string pass);\r\n}<\/pre>\n<p>Now Foo class and Bar class looks like below.<\/p>\n<pre>public class Foo : IFoo\r\n{\r\n  private IBar _bar;\r\n\r\n  public IBar Bar =&gt; _bar ?? (_bar = new Bar());\r\n\r\n  public string DoFoo()\r\n  {\r\n   return \"Foo done\";\r\n  }\r\n}\r\n\r\npublic class Bar : IBar\r\n {\r\n   public string DoBar(string id, string pass)\r\n   {\r\n     return \"Bar done\";\r\n   }\r\n}<\/pre>\n<p>Find and install Moq Nuget package, if you haven&#8217;t installed Moq yet. I&#8217;ll skip the explanation about who to use Nuget package. There are so many guides on this already.<\/p>\n<p><a href=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2017\/02\/moq1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1042\" src=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2017\/02\/moq1.jpg\" alt=\"\" width=\"454\" height=\"74\" srcset=\"https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2017\/02\/moq1.jpg 454w, https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2017\/02\/moq1-300x49.jpg 300w\" sizes=\"auto, (max-width: 454px) 100vw, 454px\" \/><\/a><\/p>\n<p>Now, let&#8217;s test simple one first.<\/p>\n<pre>using Moq;\r\n\r\n[TestMethod]\r\npublic void Test_SimpleMock()\r\n{\r\n  var mockFoo = new Mock&lt;IFoo&gt;();\r\n  var test = new MoqTest.MoqTest(mockFoo.Object);\r\n  var expected = \"Foo done\";\r\n  var actual = test.RunSimple();\r\n  Assert.AreEqual(expected, actual);\r\n}<\/pre>\n<p>This is straightforward\u00a0mocking with Moq, if you know to use Moq.<\/p>\n<p><strong>See:\u00a0<a href=\"https:\/\/github.com\/Moq\/moq4\/wiki\/Quickstart\">Moq\u00a0Quick Start<\/a><\/strong><\/p>\n<p>Next, mock recursively to mock an interface inside an interface.<\/p>\n<p>I initially expected Moq can create recursive mocking when setting up like\u00a0below.<\/p>\n<pre>var mockBar = new Mock&lt;IBar&gt;();\r\nmockFoo.Object.Bar.Object.Setup();<\/pre>\n<p>Or,\u00a0create Bar mock first and Setup mockBar and then create Foo mock, then hopefully Moq understand what I want and provide recursive mocking like below.<\/p>\n<pre> var mockBar = new Mock&lt;IBar&gt;();\r\n mockBar.Setup();\r\n var mockFoo = new Mock&lt;IFoo&gt;();<\/pre>\n<p>My instinct didn&#8217;t work and the correct solution was as below.<\/p>\n<pre>[TestMethod]\r\npublic void Test_RecursiveMock()\r\n{\r\n var mockFoo = new Mock&lt;IFoo&gt;() { DefaultValue = DefaultValue.Mock };\r\n var bar = mockFoo.Object.Bar;\r\n\r\n var barMock = Mock.Get(bar);\r\n barMock.Setup(a =&gt; a.DoBar(\"id\", \"pass\")).Returns(\"mocked\");\r\n\r\n var test = new MoqTest.MoqTest(mockFoo.Object);\r\n var expected = \"mocked\";\r\n var actual = test.Run(\"id\", \"pass\");\r\n\r\n Assert.AreEqual(expected, actual);\r\n}<\/pre>\n<p>A few noticeable points.<\/p>\n<p>Use DefaultValue.Mock option, which will instantiate all properties and method by default. If not used, only methods defined by Setup will instantiate.<\/p>\n<pre>{ DefaultValue = DefaultValue.Mock }<\/pre>\n<p>Use Mock.Get() to recursively mock Bar class and then, Setup a method with a\u00a0return value. I initially expected Moq can create recursive mocking for Bar class when<\/p>\n<p>Actually, recursive mocking using Moq is well explained in <a href=\"https:\/\/github.com\/Moq\/moq4\/wiki\/Quickstart\">Moq Quick Start<\/a>.<\/p>\n<p>However, if your application, in reality, is complex, mocking can be tricky. For instance, IFoo can be an authentication API or a third-party library.<\/p>\n<p>All test code above can be found on <a href=\"https:\/\/github.com\/rocker8942\/MoqTestInterfaceInsideInterface\">GitHub<\/a><\/p>\n<p>Moq is awesome!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction When we want to mock a method that&#8217;s inside an interface which is inside another interface. This can go tricky. I&#8217;ll have a look at easy way mocking recursively using Moq. Prerequisite Moq mocking library (\u00a0Nuget package) Case There is a class having a dependency of IFoo interface, and IFoo contains IBar interface as&hellip; <a class=\"more-link\" href=\"https:\/\/moneystock.net\/wp_e\/2017\/02\/16\/recursive-mocking-using-moq\/\">Continue reading <span class=\"screen-reader-text\">Recursive mocking using Moq<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[203],"tags":[95,241,242],"class_list":["post-1041","post","type-post","status-publish","format-standard","hentry","category-library","tag-c","tag-moq","tag-unit-test","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/1041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/comments?post=1041"}],"version-history":[{"count":4,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/1041\/revisions"}],"predecessor-version":[{"id":1047,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/1041\/revisions\/1047"}],"wp:attachment":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/media?parent=1041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/categories?post=1041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/tags?post=1041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}