Laravel-Testing-Decoded
Laravel-Testing-Decoded copied to clipboard
"Chapter 7: The solution" - missing tests + examples?
I am trying to follow the chapter, and everything goes fine until page 109 "Return Values From Mocked Methods".
Making the test testDoesNotOverwriteFile
as described works:
public function testItDoesNotOverwrite() {
$mockedFile = Mockery::mock('File');
$mockedFile->shouldReceive('exists')
->once()
->andReturn(true);
$mockedFile->shouldReceive('put')
->never();
$generator = new MyGenerator($mockedFile);
$generator->fire();
}
However:
- It breaks the original
testItWorks
test with an error:BadMethodCallException: Method Mockery_0_File::exists() does not exist on this mock object
- How to actually modify the File.php is never mentioned, as with everything else in the book. It seems like a bug. Obviously, the modification seems easy:
public function exists($path) { return file_exists($path); }
but still should be mentioned, IMO.
Am I getting something wrong, or?
I am unable to proceed due to the error Method Mockery_0_File::exists() does not exist on this mock object
- could be interesting to read more about how to handle this.
Figured out how to modify testItWorks
, if anyone else should be dealing with this:
public function testItWorks() {
$mockedFile = Mockery::mock('File');
$mockedFile
->shouldReceive('exists') // added this
->once() // added this
->shouldReceive('put')
->once()
->with('foo.txt', 'foo bar');
$generator = new MyGenerator($mockedFile);
$generator->fire();
}