SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Jan 2004
    Location
    New York
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Stumped with unit testing (mock objects and properties)

    I did a lot of research on lastcraft.com but still could not find a solution.

    Here is the gist of my class:
    PHP Code:
    class model {

    var 
    $dao//data access
    var $result//database result object

    function model(&$dao) { $this->dao =& $dao; }
    function 
    getInfo() { $this->result =& $this->dao->getInfo(); }


    Now, I am testing a page controller that uses this model. I easily made a server stub for the dao (which returns another server stub). However when I make a mock object:
    PHP Code:
    Mock::generate('model');
    $model =& new Mockmodel($this); 
    I just don't know how to make $model->result point to an object. I first just used the class itself until I realised I want to test a callCount for $model.

    Also: because it is a mock, I get this error:
    Fatal error: Call to a member function on a non-object in /.../lib/class.artcontroller.php on line 41

    which is
    PHP Code:
    $model =& new MockModel($this); 
    But I think this is because I could not set the property.

    So the question is, how can I set my mock $model->result as a reference to one of my server stubs?

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    iirc, this came up once before and the answer was just to do it.

    Something like:
    PHP Code:
    $model =& new MockModel($this);
    //some model returns or expectations
    $result =& new MockResult($this);
    //some result returns or expectations
    $model->result =& $result;

    $page->run($model);

    $result->tally();
    $model->tally(); 
    HTH
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Addict
    Join Date
    Jan 2004
    Location
    New York
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ended up making a few extra test cases but got rid of one server stub. This will do fine thanks.

  4. #4
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wouldn't sth. like the following work?
    PHP Code:
    Mock::generate('Dao');
     
    $model =& new MockModel($this);
     
    $result =& new MockResult($this);
     
    $dao =& new MockDao($this);
     
    $dao->setReturnRefernce('getInfo'$result);
     
    $dao->expectCallCount('getInfo'1);
    $page->run($model);

    $result->tally();
    $model->tally();
    $dao->tally(); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •