Fakes, Mocks and Stubs offer varying depth of business logic.
A Fake is a simple object that contains no business logic. It simply contains specific values (such as good or bad) that can be injected into a test to determine the result.
A Mock allows the test to replace a method call or property accessors to return a particular value. For example the test can mock a repository class to return pre-canned data when the Read() method is called. Mocks are also useful in unit tests as they can tested that particular methods were called and how many times they were called. You can assert against a mock.
A Stub is more comprehensive and at the coding level can replace an entire class. For the repository example the repository could be replaced with a StubRepository and this either returns a response based upon input data or based upon some preconfigured data.
Beyond the coding level a Stub can also be used to replace a subsystem. If a payments system is calling a third party interface, it may be necessary to stub out the 3rd party. This benefits if calling the third party costs money, if performance tests are required, if you want to avoid overloading the third party, or if the third party is unable to provide the failure scenarios when you need them.
For a stubbed interface it is often useful to return output data based upon input data. For a Fraud-checking stub, the response may vary based upon the applicant's last name, e.g. fail fraud checks if the last name is "MyLastName_Fail". You want to avoid statefulness in the stub or vary the response from the stub based upon configuration, which would require a system restart.
For performance testing it may also be useful to simulate latency based upon input data. In our Fraud Check example the response may delayed by 5 seconds if the applicant's first name was "MyFirstName_5s". This delay may help tease out scalability problems or race conditions in neighbouring parts of the system. The test driver can then issue a variety of load conditions simply by supplying varying usernames as request parameters.
No comments:
Post a Comment