{"id":1497,"date":"2014-02-24T22:31:17","date_gmt":"2014-02-24T22:31:17","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1497"},"modified":"2023-08-09T13:44:13","modified_gmt":"2023-08-09T13:44:13","slug":"more-moq","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/more-moq\/","title":{"rendered":"More Moq"},"content":{"rendered":"<p>In the previous post we touched on the fundamentals of using Moq, now we&#8217;ll delve a little deeper.<\/p>\n<p>Please note, MOQ version 4.20 has introduced a <a href=\"https:\/\/github.com\/moq\/moq\/releases\/tag\/v4.20.0\" rel=\"noopener\" target=\"_blank\">SponsoreLink<\/a> which appears to send data to some third party. See discussions on <a href=\"https:\/\/github.com\/moq\/moq\/issues\/1372\" rel=\"noopener\" target=\"_blank\">GitHub<\/a>. <\/p>\n<p><strong>Verifications<\/strong><\/p>\n<p>So we can verify that a method or property has been called using the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar mock = new Mock&lt;IFeed&gt;();\r\n\r\nFeedViewModel vm = new FeedViewModel(mock.Object);\r\nvm.Update();\r\n\r\nmock.Verify(f =&gt; f.Update());\r\n<\/pre>\n<p>This assumes that the <em>vm.Update()<\/em> calls the <em>IFeed.Update()<\/em>.<\/p>\n<p>But what if we are passing a mock into a view model and we want to verify a method on the mock is called but we don&#8217;t care about the specific arguments passed into the method. We can do the following (this example uses the IEventAggregator in Caliburn Micro)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar eventMock = new Mock&lt;IEventAggregator&gt;();\r\n\r\nPostListViewModel vm = new PostListViewModel(eventMock.Object);\r\n\r\neventMock.Verify(e =&gt; e.Subscribe(It.IsAny&lt;PostListViewModel&gt;()), Times.Once);\r\n<\/pre>\n<p>In the above example, PostListViewModel&#8217;s constructor is expected to call the Subscribe method on the IEventAggregator. The actual implementation passes <em>this<\/em> into the Subscribe method, but we&#8217;ll ignore the argument, except that we&#8217;re expecting it to be of type PostListViewModel. The <em>It.IsAny<PostListViewModel>()<\/em> handles this and the <em>Times.Once<\/em> simply verifies the Subscribe method was called just once.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post we touched on the fundamentals of using Moq, now we&#8217;ll delve a little deeper. Please note, MOQ version 4.20 has introduced a SponsoreLink which appears to send data to some third party. See discussions on GitHub. Verifications So we can verify that a method or property has been called using the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,31],"tags":[],"class_list":["post-1497","post","type-post","status-publish","format-standard","hentry","category-c","category-moq"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=1497"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1497\/revisions"}],"predecessor-version":[{"id":10071,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1497\/revisions\/10071"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}