Thomas,

 

Thanks for your reply.

Not sure if that would be practical:

 

Class MyClass

{

   :

   :

   public function doSomething($parameters)

   {

      :

      //do a bunch of processing

      :

      $db = new MyModel();

      :

      // set model properties

      :

      $db->save();

      :

      return some_status;

   }

   :

}

 

I would like to unit test the method doSomething. Problem is when running in 
production mode I want it to construct an object of type MyModel and actually 
save the data to the database.

However, when running a unit test, I need the line

 

$db = new MyModel();

 

to be

 

$db = new MyModelFake();

 

I see two ways around this:

 

1.       Pass in the model objects to be used either as a parameter to the 
method or in the constructor. That way I can pass in the real thing in 
production and a fake one in test mode. The problem with this is, that if you 
have a lot of dependencies you end up passing in a tone of objects. What if you 
need 3 instances of the same object? Just does not seem very “clean”.



2.       Use a factory:
$db = ModelFactory->getMyModel();
then you could just pass in a reference to the factory and when you construct 
the factory you tell it whether to construct real model objects or fake ones.

 

PHPUnit has a really cool feature for this, it will create Mock objects for you 
based on the real object, so there is not even any hand coding necessary. Does 
Lime have something like this?

 

Thanks

Henning

 

From: [email protected] [mailto:[email protected]] On 
Behalf Of Thomas Rabaix
Sent: Thursday, October 22, 2009 3:05 PM
To: [email protected]
Subject: [symfony-users] Re: Unit testing with fake model classes

 

You can open a transaction and just rollback at the end.

On Thu, Oct 22, 2009 at 8:58 PM, Henning Glatter-Gotz 
<[email protected]> wrote:

Hi,

 

I have a class I would like to unit/functional test that uses several propel 
model classes that are used to write to the db (in this scenario I am not 
getting any data out of the db). At this point I am not worried about checking 
if data actually ends up in the db, I will rely on propel to do its job.

 

So what I really want is the ability to inject fake model objects that do not 
actually write to the database when I am running a unit test. The fake model 
could be a class derived from the actual model where the save method is 
overridden and simply returns 0 (or whatever other value I want) and does 
nothing else.

 

The problem I am having is how to go about injecting the fakes. I don’t really 
want to have to pass a reference of all model objects into my object because I 
sometimes need multiple instances of the same class. So what I would need is 
for the class I am testing to know which model class to instantiate 
(MyModelReal or MyModelFake).

To follow a good design principle of decoupling dependencies it would probably 
be a good idea to not even have the model object construction inside my object 
in the first place, but rather use a factory that knows how to construct model 
objects and is environment aware (if running in test it would construct fakes 
and otherwise the ‘real’ models).

 

I know of http://components.symfony-project.org/dependency-injection/. It is 
not part of sf 1.2.x. Has anyone used this in sf 1.2.x?

Does anyone have other suggestions on how to implement something like this?

 

Thanks

Henning

 

 

 




-- 
Thomas Rabaix
http://rabaix.net



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to