Unit Testing with a Registry
Moderator: General Moderators
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Unit Testing with a Registry
Should the Registry (the well-known object that other objects can use to find other objects) be treated in unit testing as a Mock Object that returns more Mock Objects (and thus have its own parameter)?
It depends exactly what you're doing.
First off, there are interaction based tests and state based tests.
If you're using mocks you'd be doing the former, ie TDD-ing. If your also testing some other class which uses the Registry, then yes: mock the Registry and set return references for whichever objects you would expect to be returned in the context.
For a state based test, or if you're testing the Registry itself have a look at the link below. The phppatterns.com site is down but you can still find the article in the google cache (I had to put a line break in the link to stop this page pushing out too far to the right).
Marcus Baker's Registry article.
First off, there are interaction based tests and state based tests.
If you're using mocks you'd be doing the former, ie TDD-ing. If your also testing some other class which uses the Registry, then yes: mock the Registry and set return references for whichever objects you would expect to be returned in the context.
For a state based test, or if you're testing the Registry itself have a look at the link below. The phppatterns.com site is down but you can still find the article in the google cache (I had to put a line break in the link to stop this page pushing out too far to the right).
Marcus Baker's Registry article.