{"id":52,"date":"2009-02-09T22:36:29","date_gmt":"2009-02-10T05:36:29","guid":{"rendered":"http:\/\/jameskovacs.com\/2009\/02\/10\/Mocks+Vs+Stubs"},"modified":"2009-02-09T22:36:29","modified_gmt":"2009-02-10T05:36:29","slug":"mocks-vs-stubs","status":"publish","type":"post","link":"https:\/\/www.jameskovacs.com\/index.php\/2009\/02\/09\/mocks-vs-stubs\/","title":{"rendered":"Mocks vs. Stubs"},"content":{"rendered":"<p>A friend, having recently upgraded to <a href=\"http:\/\/ayende.com\/Blog\/archive\/2008\/10\/05\/rhino-mocks-3.5-rtm.aspx\">Rhino Mocks 3.5<\/a>, expressed his confusion regarding when to use mocks vs. stubs. He had read Martin Fowler\u2019s <a href=\"http:\/\/martinfowler.com\/articles\/mocksArentStubs.html\">Mocks Aren\u2019t Stubs<\/a> (recommended), but was still confused with how to actually decide whether to use a mock or a stub in practice. (For a pictorial overview, check out Jeff Atwood slightly NSFW <a href=\"http:\/\/www.codinghorror.com\/blog\/archives\/000765.html\">photo montage of dummies, fakes, stubs, and mocks<\/a>.) I thought I\u2019d share my response which cleared up the confusion for my friend\u2026  <\/p>\n<blockquote>\n<p>It&#8217;s easy to get confused. Basically, mocks specify expectation. Stubs are just stand-in objects that return whatever you give them. For example, if you were testing that invoices over $10,000 required a digital signature\u2026  <\/p>\n<div style=\"font-size: 11pt; background: white; color: black; font-family: consolas, lucida console, monospace\">\n<pre style=\"margin: 0px\"><span style=\"color: green\">\/\/ Arrange<\/span><\/pre>\n<pre style=\"margin: 0px\"><span style=\"color: blue\">var<\/span> signature = <span style=\"color: #2b91af\">DigitalSignature<\/span>.Null;<\/pre>\n<pre style=\"margin: 0px\"><span style=\"color: blue\">var<\/span> invoice = <span style=\"color: #2b91af\">MockRepository<\/span>.GenerateStub&lt;<span style=\"color: #2b91af\">IInvoice<\/span>&gt;();<\/pre>\n<pre style=\"margin: 0px\">invoice.Amount = <span style=\"color: blue\">new<\/span> <span style=\"color: #2b91af\">Money<\/span>(10001M);<\/pre>\n<pre style=\"margin: 0px\">invoice.Signature = signature;<\/pre>\n<pre style=\"margin: 0px\"><span style=\"color: blue\">var<\/span> signatureVerifier = <span style=\"color: #2b91af\">MockRepository<\/span>.GenerateMock&lt;<span style=\"color: #2b91af\">ISignatureVerifier<\/span>&gt;();<\/pre>\n<pre style=\"margin: 0px\">signatureVerifier.Expect(v =&gt; v.Verify(signature)).Return(<span style=\"color: blue\">false<\/span>);<\/pre>\n<pre style=\"margin: 0px\"><span style=\"color: blue\">var<\/span> invoiceRepository = <span style=\"color: #2b91af\">MockRepository<\/span>.GenerateMock&lt;<span style=\"color: #2b91af\">IInvoiceRepository<\/span>&gt;();<\/pre>\n<pre style=\"margin: 0px\"><span style=\"color: blue\">var<\/span> accountsPayable = <span style=\"color: blue\">new<\/span> <span style=\"color: #2b91af\">AccountsPayable<\/span>(signatureVerifier, invoiceRepository);<\/pre>\n<pre style=\"margin: 0px\">&nbsp;<\/pre>\n<pre style=\"margin: 0px\"><span style=\"color: green\">\/\/ Act <\/span><\/pre>\n<pre style=\"margin: 0px\">accountsPayable.Receive(invoice);<\/pre>\n<pre style=\"margin: 0px\">&nbsp;<\/pre>\n<pre style=\"margin: 0px\"><span style=\"color: green\">\/\/ Assert <\/span><\/pre>\n<pre style=\"margin: 0px\">invoiceRepository.AssertWasNotCalled(r =&gt; r.Insert(invoice));<\/pre>\n<pre style=\"margin: 0px\">signatureVerifier.VerifyAllExpectations(); <\/pre>\n<\/div>\n<p>I don&#8217;t have a real invoice. It&#8217;s a proxy generated by Rhino Mocks using Castle DynamicProxy. You just set\/get values on the properties. Generally I use the real object, but stubs can be handy if the real objects are complex to set up. (Then again, I would consider using an ObjectMother first.)<\/p>\n<p>Mocks on the other hand act as probes to detect behaviour. We are detecting whether the invoice was inserted into the database without requiring an actual database. We are also expecting the SignatureVerifier to be called and specifying its return value.<\/p>\n<p>Now the confusing part&#8230; You can stub out methods on mocks too. If you don&#8217;t care whether a method\/property on a mock is called (by you do care about other aspects of the mock), you can stub out just that part. <strike>You cannot however call Expect or Stub on stubs.<\/strike><\/p>\n<\/blockquote>\n<p><strong>UPDATE:<\/strong> I\u2019m including my comments inline as they respond to important points raised by Aaron and John in the comments <a href=\"http:\/\/codebetter.com\/blogs\/james.kovacs\/archive\/2009\/02\/09\/mocks-vs-stubs.aspx\">here<\/a> and many readers don\u2019t bother looking through comments. \ud83d\ude42<\/p>\n<p>@Aaron Jensen \u2013 As Aaron points out in the comments, you are really mocking or stubbing a method or property, rather than an object. The object is just a dynamically generated proxy to intercept these calls and relay them back to Rhino Mocks. Whether it\u2019s a mock\/stub\/dummy\/fake doesn\u2019t matter.<\/p>\n<p>Like Aaron, I prefer AssertWasCalled\/AssertWasNotCalled. I only use Expect\/Verify if the API requires me to supply return values from a method\/property as shown above.<\/p>\n<p>I also have to agree that Rhino Mocks, while a great mocking framework that I use everyday, is showing its age. It has at least 3 different mocking syntaxes (one of which I contributed), which increases the confusion. It\u2019s powerful and flexible, but maybe a bit too much. Rhino Mocks vNext would likely benefit from deprecating all but the AAA syntax (the one borrowed from Moq) and doing some house-cleaning on the API. I haven\u2019t given Moq an honest try since its initial release so I can\u2019t comment on it.<\/p>\n<p>@John Chapman \u2013 Thanks for the correction. I\u2019ve had Rhino Mocks throw an exception when calling Expect\/Stub on a stub. I assumed it was expected behaviour that these methods failed for stubs, but it looks like a bug. (The failure in question was part of an overly complex test and I can\u2019t repro the issue in a simple test right now. Switching from stub to mock did fix the issue though.) stub.Stub() is useful for read-only properties, but generally I prefer getting\/setting stub.Property directly. Still stub.Expect() and stub.AssertWasCalled() seems deeply wrong to me. \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A friend, having recently upgraded to Rhino Mocks 3.5, expressed his confusion regarding when to use mocks vs. stubs. He had read Martin Fowler\u2019s Mocks Aren\u2019t Stubs (recommended), but was still confused with how to actually decide whether to use a mock or a stub in practice. (For a pictorial overview, check out Jeff Atwood [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,4],"tags":[],"class_list":["post-52","post","type-post","status-publish","format-standard","hentry","category-dotnetgeneral","category-agile"],"_links":{"self":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts\/52","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":0,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}