proxi
February 28, 2011, 4:04am
1
Hi,
I’m using PHPUnit and I’m having a difficult time figuring out how to mock an object whose contructor requires a set of parameters in order to properly construct it.
$mock = $this->getMock('mnc\\util\\Point', array('getName', 'getUrl', 'getModule'));
When I run this I get an error “Missing argument 1” because the class Point requires 3 parameters in order to properly be built:
$p = new Point('nameTest', 'url/test', 'modueTest');
I’m unable to find any type of useful results from a google search or the PHPUnit manual. Any help would be nice.
Thanks!
From my limited understanding, you don’t appear to be passing the constructor arguments in - which is specified using a third argument
The third (optional) parameter may hold a parameter array that is passed to the mock object’s constructor
Depending on your PHP version, I’d recommend using Mockery instead
Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a test double framework with a succinct API capable of clearly defining all possible object operations and interactions using a human readable Domain Specific Language (DSL).
http://blog.astrumfutura.com/2010/05/the-mockery-php-mock-objects-made-simple/
often it’s just simpler to make stub classes that return the necessary values - this is easier to achieve if you use dependency injection