After having a bit of a sleep on it, I realised that the
ControllerTestCase::generate() method also mocks models, and does so in
such a way as to not loose the association with the fixtures.
Lifting code from ControllerTestCase::generate(), the $this->getMock() call
becomes:
$config = ClassRegistry::config('Model');
list($plugin, $name) = pluginSplit('EmailVerification');
$config = array_merge((array) $config, array('name' =>
'EmailVerification'));
$mock = $this->getMock('EmailVerification', array('verifyEmail'),
array($config));
ClassRegistry::removeObject('EmailVerification');
ClassRegistry::addObject('EmailVerification', $mock);
$this->EmailVerification = $mock;
This would probably make a nice convenience method on the CakeTestCase.
On Monday, 12 November 2012 17:03:43 UTC+10, Reuben wrote:
>
> I have a model method that I've written a unit test, but the method under
> test calls another method on the same model that I would like to mock. It
> seems that if I mock the method on the model, fixture information gets lost
> (that is, I think the datasource gets reset).
>
> The test is as follows:
>
> public function testUpdateVerification() {
> $this->EmailVerification = $this->getMock('EmailVerification',
> array('verifyEmail'));
>
> $record = $this->EmailVerification->read(null, 2);
>
> $this->assertEquals(2, $record['EmailVerification']['id']);
> $this->assertNull($record['EmailVerification']['order_ref']);
>
> $this->EmailVerification->expects($this->once())
> ->method('verifyEmail')
> ->will($this->returnValue(true));
>
> $this->EmailVerification->updateVerification('[email protected]', 2,
> 3);
>
> $record = $this->EmailVerification->read(null, 2);
>
> $this->assertEquals(3, $record['EmailVerification']['order_ref']);
> }
>
> The method under test is :
>
> public function updateVerification($email, $emailVerificationRef,
> $orderNumber) {
>
> $emailVerification = $this->find('first', array(
> 'conditions' => array(
> 'email' => $email,
> 'id' => $emailVerificationRef
> )
> ));
>
> if ($emailVerification) {
> // record exists, so update the order number
> $this->id = $emailVerificationRef;
> if ($this->saveField('order_ref', $orderNumber)) {
> return
> $this->verifyEmail($emailVerification['EmailVerification']['verification_ref']);
> }
> return false;
> } else {
> // no record exists, so send verification
> return $this->sendVerification($email, $orderNumber);
> }
> }
>
> As you can see, I just was to ensure that $this->verifyEmail() is called.
> I've got another test that looks after verifyEmail() specifically.
>
> Has anyone had any experience in mocking a model and ensuring that the
> fixtures get loaded, and assigned datasource stays in intact (or is
> reassigned to the mocked object)?
>
>
--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
Visit this group at http://groups.google.com/group/cake-php?hl=en.