vendredi 26 décembre 2014

Mocking public method of testing object

I have a class that has two public method. It looks something like following:



class IntRequest
{
public function updateStatus()
{
$isValid = $this->checkValidity();

// ... next is a complex logic that use $isValid
}

/**
* @return bool
*/
public function isValid()
{
// another complex logic
}
}


I need to test a first function - IntRequesr::updateStatus; however I need to run to tests. The first one with IntRequests::isValid returns false and the second one with true as a result of IntRequests::isValid


I try to mock that function but tests run with calling actual IntRequests::isValid not mocked one.


My testing code is



$intRequest = new IntRequests;

$mock = m::mock($intRequest);
$mock->shouldReceive('isValid')
->once()
->andReturn(true);

$res = $mock->updateStatus();

$this->assertTrue($res);


I've try to call $res = $intRequest->updateStatus() instead of $res = $mock->updateStatus() but with no luck.


So, I am wondering is it possible to mock function that is called inside testing method?


Aucun commentaire:

Enregistrer un commentaire