I am using Laravel 4 with PHPUnit 3.7.38. I have a class that uses a randomly generated value for input. I use str_random(100). I want to unit test it; however, everytime, I try to use shouldReceive on the method, I get an error saying that the value does not match the expected value. This makes sense since it randomly generates the value before testing it. I can not refactor the original code and I can not throw an exception halting the code because there is more to test after this code in the same function. I have tried mocking the str_random function; however, that throws another error. I'm really not sure what to do or if there is a good solution at this point in time. My code looks like:
class OriginalClass {
public function someFunction{
$randomCode = str_random(100);
anotherFunction(array($name, $lastName, $randomCode), $anotherValue)
}
}
class OriginalClassTest {
$this->MockObject
->shouldReceive('anotherFunction')
->with(array('Jon', 'Smith', '11155488321'), '49')
->andReturn('Output');
$this->assertEquals('Output', $this
->MockObject
->anotherFunction(array('Jon', 'Smith', '11155488321'), '49'));
}
By the time the function is called a newly generated $randomCode has been created and I get the error
"Mockery\Exception\NoMatchingExpectationException : No matching handler found for Mockery_1_MockObject::anotherFunction(array('Jon', 'Smith', 'newly generated randomCode'), '49')"
Aucun commentaire:
Enregistrer un commentaire