I'm trying to test a controller and I want to isolate it from the models it uses.
For doing so, I want to mock my Factory class (the one that instantiates all the models) and some models.
The example below mocks only one model and tries to return it everytime the Factory is called to instantiate it.
...
// Mocked model.
$mockUsersEditprofileModel = $this->getMock( 'UsersEditprofileModel', array( 'checkCurrentEmailPass' ) );
$mockUsersEditprofileModel->expects( $this->any() )
->method( 'checkCurrentEmailPass' )
->will( $this->returnValue( true ) );
// Mocked Factory object
// Here is where I get the problems.
$supFactory = $this->getMock( 'SuperFactory', array( 'getClass' ), array( $this->config ) );
$supFactory->expects( $this->any() )
->method( 'getClass' )->with( $this->equalTo('UsersEditprofileModel') )
->will( $this->returnValue( $mockUsersEditprofileModel ) );
$this->obj = new UsersCancelationController();
$this->obj->setInstance();
$this->obj->setFactory( $supFactory ); // Here I replace the current Factory for the mocked one.
...
When I execute this, I receive the following error:
Fail: "testRightObjectCreated" -> Expectation failed for method name is equal to when invoked zero or more times. Mocked method does not exist.
I think the problem comes when I use "->with( $this->equalTo('UsersEditprofileModel') )" in the $supFactory. There's no error when I skip the with part and I leave it with expects()->method()->will()... but of course it doesn't work for me because I want the Factory class to instantiate many models, not always the one I've mocked.
Any suggestions?
