PHPUnit's Mock Objects system is a little weird. What I frequently do is 
implement a total or clean Mock where everything is mocked, including abstract 
methods. I also disable the constructor. Once you figure out how to do that, 
the rest is easy. Partial Mocks need something a bit different but I don't use 
partials very often. Here's a method I add somewhere in a test helper method to 
make this easy.

protected function _getCleanMock($className, $testClassName) {
    $class = new ReflectionClass($className);
    $methods = $class->getMethods();
    $stubMethods = array();
    foreach ($methods as $method) {
        if ($method->isPublic() || ($method->isProtected()
        && $method->isAbstract())) {
            $stubMethods[] = $method->getName();
        }
    }
    $mocked = $this->getMock(
        $className,
        $stubMethods,
        array(),
        $className . '_' . $testClassName . '_' . uniqid(),
        false
    );
    return $mocked;
}

It's safe to use from a setup() method since it creates a unique Mock name on 
every call.

 Pádraic Brady

http://blog.astrumfutura.com
http://www.survivethedeepend.com
OpenID Europe Foundation Irish Representative





________________________________
From: wenbert <[email protected]>
To: [email protected]
Sent: Tuesday, August 25, 2009 4:39:19 AM
Subject: [fw-general] Creating a dummy object of Zend_Oauth_Token_Access for 
Unit Testing


Hello,

I am just getting into unit testing. I am testing out my application. It
uses Zend_Oauth and Zend_Service_Twitter.

I have a model which requires me to feed it an instance of
Zend_Oauth_Token_Access. The problem is, how do I create a dummy for that
intance?

I have the following below:
----------------------------------
    $oauth_token_stub = $this->getMock('Zend_Oauth',
array('getAccessToken'),array(),'',FALSE);
    $oauth_token_stub->expects($this->once())->method('getAccessToken');
----------------------------------

The error I get from PHPUnit is:
----------------------------------
1) testuserTimeline(Model_TwitterTest)
Argument 1 passed to Twitter_Model_Twitter::__construct() must be an
instance of Zend_Oauth_Token_Access, instance of Mock_Zend_Oauth_8e38a582
given, called in /htdocs/twitter/tests/application/models/TwitterTest.php on
line 63 and defined
/htdocs/twitter/application/models/Twitter.php:15
/htdocs/twitter/tests/application/models/TwitterTest.php:63
----------------------------------
NOTE: that Argument 1 is is $oauth_token_stub I am trying to pass...

Any ideas?

Thanks,
Wenbert

EDIT: edited the code and error message to latest

-----
http://blog.ekini.net
-- 
View this message in context: 
http://www.nabble.com/Creating-a-dummy-object-of-Zend_Oauth_Token_Access-for-Unit-Testing-tp25127669p25127669.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to