I'm facing some difficulties to mock a registry-like object in my tests, this might be because of a wrong design but it always end using $this->at(x) and, IMHO, therefore tightly coupling my test with tested code.
For example, my registry-like object looks like something like this ( remember that its not real code, juste an example ) :
class registrylike{
public function set($k,$v){$this->$k=$v;return $this;}
public function get($v){return $this->$v;}
}
let say my to be tested class is :
class main{
protected $registrylike;
public function get_ready($registrylike){
$this->registrylike = $registrylike;
$this->registrylike->set('k1','v1');
$this->registrylike->set('k2','v2');
}
public function do_something(){
return sprintf('%s%s',$this->registrylike->get('k1'),$this->registrylike->get('k2'));
}
}
and my (simple) test is :
class mainTest extends PHPUnit_Framework_TestCase {
protected $tested;
public function setUp() {
$this->tested= new main;
$registrymock = $this->getMock('registrylike');
$registrymock->expects($this->at(2))
->method('get')
->with('k1')
->will($this->returnValue('v1'));
$registrymock->expects($this->at(3))
->method('get')
->with('k2')
->will($this->returnValue('v2'));
$this->tested->get_ready($registrymock);
}
public function test_do_something(){
$this->AssertEquals($this->tested->do_something(),'v1v2');
}
}
Is there any way to avoid using $this->at(x)? Should I reconsider this design or use a "real" registrylike instance instead of a mocked one?
Thanks in advance for your answers
Aucun commentaire:
Enregistrer un commentaire