<p>Many people use Mock Objects when they are writing unit tests. What is a <strong>Mock Object</strong>? Why would I ever need one? Do I need a Mock Object Framework?</p>

[{'answer_id': 4316, 'author': 'Ed.', 'author_id': 522, 'author_profile': 'https://Stackoverflow.com/users/522', 'pm_score': 3, 'selected': False, 'text': '<p>It allows you to test how one part of your project interacts with the rest, without building the entire thing and potentially missing a vital part.</p>\n\n<p>EDIT: Great example from wikipedia: It allows you to test out code beforehand, like a car designer uses a crash test dummy to test the behavior of a car during an accident.</p>\n'}, {'answer_id': 4317, 'author': 'Nick Berardi', 'author_id': 17, 'author_profile': 'https://Stackoverflow.com/users/17', 'pm_score': 3, 'selected': False, 'text': '<p>Object Mocking is a way to create a "virtual" or mocked object from an interface, abstract class, or class with virtual methods.  It allows you to sort of wrap one of these in your own definition for testing purposes.  It is useful for making an object that is relied on for a certain code block your are testing.</p>\n\n<p>A popular one that I like to use is called <a href="http://code.google.com/p/moq/" rel="noreferrer">Moq</a>, but there are many others like RhinoMock and numerous ones that I don\'t know about.</p>\n'}, {'answer_id': 4318, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 4, 'selected': False, 'text': "<p>A mock object lets you test against just what you are writing, and abstract details such as accessing a resource (disk, a network service, etc).  The mock then lets you pretend to be that external resource, or class or whatever.</p>\n\n<p>You don't really need a mock object framework, just extend the class of the functionality you don't want to worry about in your test and make sure the class you are testing can use your mock instead of the real thing (pass it in via a constructor or setter or something.</p>\n\n<p>Practice will show when mocks are helpful and when they aren't.</p>\n\n<p>EDIT: Mocking resources is especially important so you don't have to rely on them to exist during the test, and you can mock the details of how they exist and what they respond (such as simulating a FileNotFoundException, or a webservice that is missing, or various possible return values of a webservice)... all without the slow access times involved (mocking will prove MUCH faster than accessing such resources in the test).</p>\n"}, {'answer_id': 4322, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 6, 'selected': True, 'text': '<p>Object Mocking is used to keep dependencies out of your unit test.\nSometimes you\'ll have a test like "SelectPerson" which will select a person from the database and return a Person object. </p>\n\n<p>To do this, you would normally need a dependency on the database, however with object mocking you can simulate the interaction with the database with a mock framework, so it might return a dataset which looks like one returned from the database and you can then test your code to ensure that it handles translating a dataset to a person object, rather than using it to test that a connection to the database exists.</p>\n'}, {'answer_id': 4326, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 2, 'selected': False, 'text': "<p>Another use is it will let you test against other parts of your system that aren't built yet.  For example, if your class depends on some other class that is part of a feature that someone else is working on, you can just ask for a mostly complete interface, program to the interface and just mock the details as you expect them to work.  Then, make sure your assumptions about the interface were correct (either while you are developing, or once the feature is complete).</p>\n"}, {'answer_id': 4329, 'author': 'Joshua McKinnon', 'author_id': 235, 'author_profile': 'https://Stackoverflow.com/users/235', 'pm_score': 4, 'selected': False, 'text': "<p>Several people have already answered the 'what', but here are a couple of quick 'whys' that I can think of:</p>\n\n<ol>\n<li><p><strong>Performance</strong></p>\n\n<p>Because unit tests should be fast, testing a component that\ninteracts with a network, a database, or other time-intensive\nresource does not need to pay the penalty if it's done using mock\nobjects. The savings add up quickly.</p></li>\n<li><p><strong>Collaboration</strong></p>\n\n<p>If you are writing a nicely encapsulated piece of\ncode that needs to interact with someone else's code (that hasn't\nbeen written yet, or is in being developed in parallel - a common\nscenario), you can exercise your code with mock objects once an\ninterface has been agreed upon. Otherwise your code may not begin to\nbe tested until the other component is finished.</p></li>\n</ol>\n"}, {'answer_id': 4383, 'author': 'Brad Tutterow', 'author_id': 308, 'author_profile': 'https://Stackoverflow.com/users/308', 'pm_score': 3, 'selected': False, 'text': '<blockquote>\n  <p>Do I need a Mock Object Framework?</p>\n</blockquote>\n\n<p>Certainly not. Sometimes, writing mocks by hand can be quite tedious. But for simple things, it\'s not bad at all.  Applying the principle of <a href="http://www.codinghorror.com/blog/archives/000705.html" rel="noreferrer">Last Responsible Moment</a> to mocking frameworks, you should only switch from hand-written mocks to a framework when you\'ve proven to yourself that hand-writing mocks is more trouble than it\'s worth.</p>\n\n<p>If you\'re just getting starting with mocking, jumping straight into a framework is going to at least double your learning curve (can you double a curve?).  Mocking frameworks will make <strong>much</strong> more sense when you\'ve spent a few projects writing mocks by hand.</p>\n'}, {'answer_id': 4391, 'author': 'Curt Hagenlocher', 'author_id': 533, 'author_profile': 'https://Stackoverflow.com/users/533', 'pm_score': 1, 'selected': False, 'text': "<p>Whether or not you a mocking framework is useful depends in part on the language of the code you're writing.  With a static language, you need to put in extra effort in order to trick the compiler into accepting your mock objects as a replacement for the real thing.  In a dynamically-typed language such as Python, Ruby or Javascript, you can generally just attach the methods onto arbitrary object or class and pass that as the parameter -- so a framework would add much less value.</p>\n"}, {'answer_id': 204978, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>2 recommended mocking frameworks for .net Unit testing are Typemock Isolator and Rhino Mock.</p>\n\n<p>In the following link you can see an explanation from Typemock as to why you need a <a href="http://www.typemock.com/Docs/writing_unit_tests_with_isolator.html" rel="nofollow noreferrer">mocking framework</a> for Unit Testing.</p>\n'}]
['https://Stackoverflow.com/questions/4314', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/358/']