What are mock objects?http://forums.asp.net/t/1797721.aspx/1?What+are+mock+objects+Sat, 28 Apr 2012 15:27:17 -040017977214954947http://forums.asp.net/p/1797721/4954947.aspx/1?What+are+mock+objects+What are mock objects? Hi all, I am starting to read about MVC and I came across &quot;mock&quot; objects. Can anyone tell me in a nutshell what a mock object is? Thanks, 2012-04-27T13:02:21-04:004954950http://forums.asp.net/p/1797721/4954950.aspx/1?Re+What+are+mock+objects+Re: What are mock objects? <p>A mock object is a class that you setup to behave like another piece of software, often to avoid the need to call a real web service or perhaps if the thing you need to call has not been written yet. The thing that is cool is that you can tell the mock how to behave to help you with testing. You could say (in pseudo code) if I call GetData() with an argument of A then return data XYZ.&nbsp;</p> <p>There therefore help with testing where you don't need to worry about how another piece of code behaves.</p> 2012-04-27T13:04:50-04:004954958http://forums.asp.net/p/1797721/4954958.aspx/1?Re+What+are+mock+objects+Re: What are mock objects? <p>A mock is a piece of infrastructure used when writing unit tests.</p> <p>The problem is that you want to test component &quot;A&quot; but it calls into component &quot;B&quot;. When you're doing unit testing you don't want both to be called, so the dependency on &quot;B&quot; is a problem for isolating &quot;A&quot;. If the code is designed properly (meaning there is loose coupling between A &amp; B typically done with interfaces) then during your unit test you can substitute a &quot;fake&quot; implementation of &quot;B&quot; so that when your unit test runs and calls &quot;A&quot; then when &quot;A&quot; calls into &quot;B&quot; it's calling your controlled &quot;fake&quot; code for &quot;B&quot;.</p> <p>So a mock can mean many things depending on how hard core you are with unit testing, but for me it's basically a library that can easily provide that &quot;fake&quot; dependency &quot;B&quot; I used in my above example.</p> <p>This isn't the best explanation, but hopefully it helps a bit :)</p> 2012-04-27T13:10:00-04:004955042http://forums.asp.net/p/1797721/4955042.aspx/1?Re+What+are+mock+objects+Re: What are mock objects? But is &quot;mock&quot; a reserved word, or is just a class created by you? Thanks for your answers 2012-04-27T13:50:24-04:004955052http://forums.asp.net/p/1797721/4955052.aspx/1?Re+What+are+mock+objects+Re: What are mock objects? <p>its terminology used for the object of these classes.</p> 2012-04-27T13:53:03-04:004955215http://forums.asp.net/p/1797721/4955215.aspx/1?Re+What+are+mock+objects+Re: What are mock objects? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>LAM_</h4> But is &quot;mock&quot; a reserved word, or is just a class created by you? Thanks for your answers </blockquote> <p></p> <p>Mock is not a reserved word. As the others have pointed out, It's just the description of your custom objects or libraries that are used to simulate interacting with other systems (databases, web services, etc).</p> <p>One of the big things with unit testing is that you don't want to cross boudnaries, you don't want to touch a database or actually talk to a web service because your test will have an external dependency. If the web service goes down your test will fail, you don't want to depend on it. Your test isn't testing the web service, but your own code. So you want to &quot;mock&quot; these external objects so that you have full control over the environment. It also makes your tests faster.</p> 2012-04-27T15:55:02-04:004955593http://forums.asp.net/p/1797721/4955593.aspx/1?Re+What+are+mock+objects+Re: What are mock objects? <p>Reserve some time to play with <a href="http://code.google.com/p/moq/wiki/QuickStart"> Moq library</a>&nbsp;:)&nbsp;it is pretty simple and just great. Moq is available in NuGet feed.</p> <p>You can also look at <a href="http://martinfowler.com/articles/mocksArentStubs.html"> this article</a> by Martin Fowler for some background.</p> 2012-04-27T22:24:03-04:004956297http://forums.asp.net/p/1797721/4956297.aspx/1?Re+What+are+mock+objects+Re: What are mock objects? <p><a href="http://nsubstitute.github.com/">nSubstitute</a>&nbsp;FTW</p> 2012-04-28T15:27:17-04:00