Hi Stephen,

2009/1/16 isleshocky77 <[email protected]>:
> One of my model methods takes an sfUser as a parameter so I need to
> create and user an sfUser in my unit testing.  What is the best way of
> doing this?

When unit testing any code, you should (almost) always pass mock
objects into the tested code. The purpose of unit tests is to test a
single piece of code in an isolated environment.

Now imagine, your class "Record" depends on sfUser, which depends on
sfEventDispatcher and sfSessionStorage, and there is an error in
sfSessionStorage, the test for your class "Record" will fail, although
there might not be a single error in class "Record" itself.

This is why you should implement a mock object inside your unit test,
that has exactly the interface that your class "Record" needs. You
don't even need to implement the methods, they can be empty stubs as
long as that's enough for class "Record" to work. If the methods of
the mock class should return a specific value for a specific input
parameter, you can just check for that parameter and return the value
statically. Leon posted a nice example for this.

The big advantage is that your unit test now really only tests your
class, but not any others it depends on. This makes finding errors
based on failing tests much easier.

Hope I could help you.


Bernhard

--~--~---------~--~----~------------~-------~--~----~
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