-- davidwinterbottom <[EMAIL PROTECTED]> wrote
(on Tuesday, 24 June 2008, 11:45 PM -0700):
> When you have an active record style model that uses a db adapter from a
> registry, what's the best practise for unit testing? Do you use a mock
> object in the registry, use a test database, clean up the dev database after
> inserting new records, or something else?
>
> For instance, my User model has a save method like...
>
> class User ....
>
> public function save() {
> ...
> $Db = Zend_Registry::get(REGISTRY_DB);
> if ($this->id) {
> $Db->update(self::TABLE, $attributes, 'UserId='.$this->id);
> } else {
> $attributes['DateCreated'] = date(MYSQL_DATE_FORMAT);
> $Db->insert(self::TABLE, $attributes);
> $this->id = $Db->lastInsertId();
> }
> }
>
> and so, as the db adapter is held in a registry, it is not as
> straightforward as normal to pass in a mock object for the db adapter. How
> do other people manage this situation (which must be awfully common)? Do
> you just allow yourself to write to the normal db and clean up afterwards
> (so there aren't loads of test users in there)? Or use a special testing db
> so the mess doesn't matter? Or something I've not thought of?
I usually use one of the following approaches:
* Test DB (usually sqlite, so I don't have to depend on network access
or a running DB server on my machine).
* In-memory sqlite database. This usually requires a little more setup
in your test harness, but the speed is unbeatable.
If you look at how Rails recommends testing, both of the above fit in
with their recommendations -- separate dataset for testing than for
production.
--
Matthew Weier O'Phinney
Software Architect | [EMAIL PROTECTED]
Zend Framework | http://framework.zend.com/