Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed an advice for unit testing using mock object
    primarykey
    data
    text
    <p>I just recently read about "Mocking objects" for unit testing and currently I'm having a difficulties implementing this approach in my application. Please let me explain my problem.</p> <p>I have a <code>User</code> model class, which is dependent on 2 data sources (database and facebook web service). The controller class simply use this <code>User</code> model as an interface to access data and it doesn't care about where the data came from.</p> <p>Currently I never done any unit test to this <code>User</code> model because it is dependent on an external web service. But just a while ago, I read about object mocking and now I know that it is a common approach to unit test a class that depends on external resources (like in my case).</p> <p>Now I want to create a unit test for the <code>User</code> model, but then I encountered a design issue: In order for the <code>User</code> model to use a mocked Facebook SDK, I have to inject this mocked Facebook SDK to the User object (probably using a setter). Therefore I can't construct the Facebook SDK inside the User object. I have to <strong>construct it outside</strong> the User object, and <strong>inject</strong> the SDK into the User object.</p> <p>The real client of my <code>User</code> model is the application's controller. Therefore I have to construct the Facebook SDK inside the controller and inject it to the user object. Well, this is a problem because I want my controller to be as clean as possible. I want my controller to be ignorant about the application's data source.</p> <p>I'm not good at explaining something systematically, so you'll probably sleeping before reading this last paragraph. But anyway, I want to ask if anyone here ever encountered the same problem as mine? How do you solve this problem?</p> <p>Regards, Andree</p> <p>P.S: I'm using Zend framework, PHP 5.3.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload