Ajaxiome
Ajaxiome

Reputation: 1

Using Laravel and Mockery

i just try to go in this : I have a class A which uses services of class B. To isolate this class A and to test it I would like to use Mockery and stub class B.

To do so I did

public function testMock()
{

    $driver = Mockery::mock('Driver');
    App::instance('Driver',$driver);
    $driver->shouldReceive('get')->once()->andReturn('Did the job');

    $request = new BSRequest($driver);

    $this->assertEquals($request->get(),'Did the job');

}

But i get always this message after running test ErrorException: Argument 1 passed to BSrequest::__construct() must be an instance of Driver, instance of Mockery_0_Library_Driver given, called in /var/www/laravel/app/tests/ExampleTest.php on line 56 and defined

And my BSrequest is just this:

    class BSrequest {

private $driver;

public function __construct(Driver $driver) {
    $this->driver = $driver;
}

function get() {
    return $this->driver->get();
}}

Could you tell me how to achieve this ? Thanks

Upvotes: 0

Views: 187

Answers (2)

awei
awei

Reputation: 1164

I believe you're running into a namespacing issue OR you're running into an autoloading issue.

In your test file, ensure that the Driver class has been autoloaded. You can do something like this:

public function testDriverClassIsAvailable()
{

    $driver = new Driver();

    $this->assertEquals(get_class($driver), 'Driver');
}

If this works, then you will know that your Driver class is autoloaded and available and there is an issue with the BSRequest class because it does not have a Driver class available. If you're using namespaces, ensure that there are no typos.

A mocked Driver instance should satisfy the type-hinting required by the BSRequest->__construct() so you should definitely include the typehint.

Upvotes: 1

Ajaxiome
Ajaxiome

Reputation: 1

In fact every things work great when writing

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

That's mean removing the Type control on $driver. But I'm sure that we can achieve all this stuff keeping Driver $driver

Unfortunatly I'm unable to know how

Upvotes: 0

Related Questions