Mockery & PHPUnit: method does not exist on this mock object

PeraMika

Can you tell me where's the problem? I have a file GeneratorTest.php with the following tests:

<?php

namespace stats\Test;

use stats\jway\File;
use stats\jway\Generator;

class GeneratorTest extends \PHPUnit_Framework_TestCase
{

    public function tearDown() {
        \Mockery::close();
    }

    public function testGeneratorFire()
    {
        $fileMock = \Mockery::mock('\stats\jway\File');
        $fileMock->shouldReceive('put')->with('foo.txt', 'foo bar')->once();
        $generator = new Generator($fileMock);
        $generator->fire();
    }

    public function testGeneratorDoesNotOverwriteFile()
    {
        $fileMock = \Mockery::mock('\stats\jway\File');
        $fileMock->shouldReceive('exists')
            ->once()
            ->andReturn(true);

        $fileMock->shouldReceive('put')->never();

        $generator = new Generator($fileMock);
        $generator->fire();
    }
}

and here are File and Generator classes:

File.php:

class File
{
    public function put($path, $content)
    {
        return file_put_contents($path, $content);
    }

    public function exists($file_path)
    {
        if (file_exists($file_path)) {
            return true;
        }
        return false;
    }
}

Generator.php:

class Generator
{
    protected $file;

    public function __construct(File $file)
    {
        $this->file = $file;
    }

    protected function getContent()
    {
        // simplified for demo
        return 'foo bar';
    }

    public function fire()
    {
        $content = $this->getContent();
        $file_path = 'foo.txt';

        if (! $this->file->exists($file_path)) {
            $this->file->put($file_path, $content);
        }
    }

}

So, when I run these tests, I get the following message: BadMethodCallException: Method ... ::exists() does not exist on this mock object.

enter image description here

Bram Gerritsen

The error message seems clear to me. You only did setup a expectation for the put method, but not exists. The exists method is called by the class under test in all code paths.

public function testGeneratorFire()
{
    $fileMock = \Mockery::mock('\stats\jway\File');
    $fileMock->shouldReceive('put')->with('foo.txt', 'foo bar')->once();

    //Add the line below
    $fileMock->shouldReceive('exists')->once()->andReturn(false);

    $generator = new Generator($fileMock);
    $generator->fire();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mockery & PHPUnit: method does not exist on this mock object

From Dev

method does not exist on this mock object - Laravel , Mockery

From Dev

How to mockery mock internal method

From Dev

Mockery mock method inside closure

From Dev

Is there a method like PHPUnit's at method in Mockery?

From Dev

Mock a class method using mockery and sinon

From Dev

Mockery "shouldReceive" yet method doesn't exist

From Dev

Mock object in PHPUnit is not invoked

From Dev

Mockery: how does a passive partial mock differ from the default mock?

From Dev

phpunit mock method called in constructor

From Dev

phpunit: mock object not fooling php

From Dev

What is the sense of a mock object in PHPUnit?

From Dev

Determining whether or not an object is a PHPUnit mock

From Dev

phpunit: mock object not fooling php

From Dev

Determining whether or not an object is a PHPUnit mock

From Dev

Override issue: does exist a method in Object to override?

From Dev

Rspec, can you stub a method that doesn't exist on an object (or mock an object that can take any method)?

From Dev

PHPUnit: Mock a method to behave like identity

From Dev

phpunit mock expectation failed for method name is equal

From Dev

PHPUnit fatal error call to undefined method on mock

From Dev

PHPUnit - disable original constructor in Mockery

From Dev

PHPUnit : Assert a parameter when pass it to mock object

From Dev

phpunit - mockbuilder - set mock object internal property

From Dev

PHPUnit : Assert a parameter when pass it to mock object

From Dev

phpunit mock does not return assigned value

From Dev

phpunit mock does not return assigned value

From Dev

Method "currentAncestor" for object "Knp\Menu\MenuItem" does not exist in Sonata

From Dev

PHPUnit annotation throwing ReflectionException: Class does not exist

From Dev

Laravel mock with Mockery Eloquent models

Related Related

  1. 1

    Mockery & PHPUnit: method does not exist on this mock object

  2. 2

    method does not exist on this mock object - Laravel , Mockery

  3. 3

    How to mockery mock internal method

  4. 4

    Mockery mock method inside closure

  5. 5

    Is there a method like PHPUnit's at method in Mockery?

  6. 6

    Mock a class method using mockery and sinon

  7. 7

    Mockery "shouldReceive" yet method doesn't exist

  8. 8

    Mock object in PHPUnit is not invoked

  9. 9

    Mockery: how does a passive partial mock differ from the default mock?

  10. 10

    phpunit mock method called in constructor

  11. 11

    phpunit: mock object not fooling php

  12. 12

    What is the sense of a mock object in PHPUnit?

  13. 13

    Determining whether or not an object is a PHPUnit mock

  14. 14

    phpunit: mock object not fooling php

  15. 15

    Determining whether or not an object is a PHPUnit mock

  16. 16

    Override issue: does exist a method in Object to override?

  17. 17

    Rspec, can you stub a method that doesn't exist on an object (or mock an object that can take any method)?

  18. 18

    PHPUnit: Mock a method to behave like identity

  19. 19

    phpunit mock expectation failed for method name is equal

  20. 20

    PHPUnit fatal error call to undefined method on mock

  21. 21

    PHPUnit - disable original constructor in Mockery

  22. 22

    PHPUnit : Assert a parameter when pass it to mock object

  23. 23

    phpunit - mockbuilder - set mock object internal property

  24. 24

    PHPUnit : Assert a parameter when pass it to mock object

  25. 25

    phpunit mock does not return assigned value

  26. 26

    phpunit mock does not return assigned value

  27. 27

    Method "currentAncestor" for object "Knp\Menu\MenuItem" does not exist in Sonata

  28. 28

    PHPUnit annotation throwing ReflectionException: Class does not exist

  29. 29

    Laravel mock with Mockery Eloquent models

HotTag

Archive