Sunday, November 11, 2012

Unit testing models and mocking

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('verification@test.com', 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 cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate