词条 | Mock object |
释义 |
In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways, most often as part of a software testing initiative. A programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to simulate the dynamic behavior of a human in vehicle impacts. The technique is also applicable in generic programming. MotivationIn a unit test, mock objects can simulate the behavior of complex, real objects and are therefore useful when a real object is impractical or impossible to incorporate into a unit test. If an object has any of the following characteristics, it may be useful to use a mock object in its place:
For example, an alarm clock program which causes a bell to ring at a certain time might get the current time from a time service. To test this, the test must wait until the alarm time to know whether it has rung the bell correctly. If a mock time service is used in place of the real time service, it can be programmed to provide the bell-ringing time (or any other time) regardless of the real time, so that the alarm clock program can be tested in isolation. Technical detailsMock objects have the same interface as the real objects they mimic, allowing a client object to remain unaware of whether it is using a real object or a mock object. Many available mock object frameworks allow the programmer to specify which, and in what order, methods will be invoked on a mock object and what parameters will be passed to them, as well as what values will be returned. Thus, the behavior of a complex object such as a network socket can be mimicked by a mock object, allowing the programmer to discover whether the object being tested responds appropriately to the wide variety of states such mock objects may be in. Mocks, fakes, and stubsClassification between mocks, fakes, and stubs is highly inconsistent across the literature.[1][2][3][4][5][6] Consistent among the literature, though, is that they all represent a production object in a testing environment by exposing the same interface. Which out of mock, fake, or stub is the simplest is inconsistent, but the simplest always returns pre-arranged responses (as in a method stub). On the other side of the spectrum, the most complex object will fully simulate a production object with complete logic, exceptions, etc. Whether or not any of the mock, fake, or stub trio fits such a definition is, again, inconsistent across the literature. For example, a mock, fake, or stub method implementation between the two ends of the complexity spectrum might contain assertions to examine the context of each call. For example, a mock object might assert the order in which its methods are called, or assert consistency of data across method calls. In the book The Art of Unit Testing[7] mocks are described as a fake object that helps decide whether a test failed or passed by verifying whether an interaction with an object occurred. Everything else is defined as a stub. In that book, fakes are anything that is not real, which, based on their usage, can be either stubs or mocks. Setting expectationsConsider an example where an authorization sub-system has been mocked. The mock object implements an Similarly, mock-only settings could ensure that subsequent calls to the sub-system will cause it to throw an exception, hang without responding, or return Writing log stringsA mock database object's This is a missed opportunity. The mock method could add an entry to a public log string. The entry need be no more than "Person saved",[9]{{rp|146–7}} or it may include some details from the person object instance, such as a name or ID. If the test code also checks the final contents of the log string after various series of operations involving the mock database then it is possible to verify that in each case exactly the expected number of database saves have been performed. This can find otherwise invisible performance-sapping bugs, for example, where a developer, nervous of losing data, has coded repeated calls to Use in test-driven developmentProgrammers working with the test-driven development (TDD) method make use of mock objects when writing software. Mock objects meet the interface requirements of, and stand in for, more complex real ones; thus they allow programmers to write and unit-test functionality in one area without calling complex underlying or collaborating classes.[9]{{rp|144–5}} Using mock objects allows developers to focus their tests on the behavior of the system under test without worrying about its dependencies. For example, testing a complex algorithm based on multiple objects being in particular states can be clearly expressed using mock objects in place of real objects. Apart from complexity issues and the benefits gained from this separation of concerns, there are practical speed issues involved. Developing a realistic piece of software using TDD may easily involve several hundred unit tests. If many of these induce communication with databases, web services and other out-of-process or networked systems, then the suite of unit tests will quickly become too slow to be run regularly. This in turn leads to bad habits and a reluctance by the developer to maintain the basic tenets of TDD. When mock objects are replaced by real ones, the end-to-end functionality will need further testing. These will be integration tests rather than unit tests. LimitationsThe use of mock objects can closely couple the unit tests to the implementation of the code that is being tested. For example, many mock object frameworks allow the developer to check the order of and number of times that mock object methods were invoked by the real object being tested; subsequent refactoring of the code that is being tested could therefore cause the test to fail even though all mocked object methods still obey the contract of the previous implementation. This illustrates that unit tests should test a method's external behavior rather than its internal implementation. Over-use of mock objects as part of a suite of unit tests can result in a dramatic increase in the amount of maintenance that needs to be performed on the tests themselves during system evolution as refactoring takes place. The improper maintenance of such tests during evolution could allow bugs to be missed that would otherwise be caught by unit tests that use instances of real classes. Conversely, simply mocking one method might require far less configuration than setting up an entire real class and therefore reduce maintenance needs. Mock objects have to accurately model the behavior of the object they are mocking, which can be difficult to achieve if the object being mocked comes from another developer or project or if it has not even been written yet. If the behavior is not modelled correctly then the unit tests may register a pass even though a failure would occur at run time under the same conditions that the unit test is exercising, thus rendering the unit test inaccurate.[10] See also{{Portal|Software Testing}}
References1. ^{{cite web|url=https://msdn.microsoft.com/en-us/library/ff798400.aspx|title=Stubs and Mocks|publisher=}} 2. ^{{cite web|url=http://hamletdarcy.blogspot.ca/2007/10/mocks-and-stubs-arent-spies.html|title=behind the times: Mocks and Stubs aren't Spies|publisher=}} 3. ^{{cite web|url=http://xunitpatterns.com/Mocks,%20Fakes,%20Stubs%20and%20Dummies.html|title=Mocks, Fakes, Stubs and Dummies at XUnitPatterns.com|publisher=}} 4. ^{{cite web|url=https://stackoverflow.com/questions/3459287/whats-the-difference-between-a-mock-stub?lq=1|title=What's the difference between a mock & stub?|publisher=}} 5. ^{{cite web|url=https://stackoverflow.com/questions/346372/whats-the-difference-between-faking-mocking-and-stubbing|title=What's the difference between faking, mocking, and stubbing?|publisher=}} 6. ^{{cite book |last=Feathers |first=Michael |title=Working effectively with legacy code |year=2005 |publisher=Prentice Hall |location=NJ |isbn=0-13-117705-2 |chapter=Sensing and separation |page=23 et seq }} 7. ^{{cite book |last=Osherove |first=Roy |title=The art of unit testing |year=2009 |publisher=Manning |isbn=978-1-933988-27-6 |chapter=Interaction testing with mock objects et seq }} 8. ^These examples use a nomenclature that is similar to that used in Unified Modeling Language 9. ^1 {{cite book |last=Beck |first=Kent |title=Test-Driven Development By Example |year=2003 |publisher=Addison Wesley |location=Boston |isbn=0-321-14653-0}} 10. ^InJava.com to Mocking | O'Reilly Media External links
|title=A Brief History of Mock Objects |url=http://www.mockobjects.com/2009/09/brief-history-of-mock-objects.html |date=8 September 2009 |publisher=Mockobjects.com/ |author=Tim Mackinnon }}
|url=http://www.ibm.com/developerworks/rational/library/oct06/pollice/ |title=Using mock objects for complex unit tests |publisher=IBM developerWorks |date=16 October 2006 |archiveurl=https://web.archive.org/web/20070504084844/http://www.ibm.com/developerworks/rational/library/oct06/pollice/ |archivedate=4 May 2007 }}
5 : Object (computer science)|Unit testing|Software design patterns|Extreme programming|Source code |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。