Laravel-Testing-Decoded icon indicating copy to clipboard operation
Laravel-Testing-Decoded copied to clipboard

"Chapter 7: The solution" - missing tests + examples?

Open simplenotezy opened this issue 10 years ago • 1 comments

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:

  1. It breaks the original testItWorks test with an error: BadMethodCallException: Method Mockery_0_File::exists() does not exist on this mock object
  2. 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.

simplenotezy avatar Dec 16 '14 19:12 simplenotezy

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();
}

simplenotezy avatar Dec 16 '14 19:12 simplenotezy