Mock lessons in Laravel 5.6

0

Good morning everyone, I have the following problem when trying to mock a class for Laravel 5.6, I explain

I have a class that helps me verify certain things to use in my controllers, but since I use the Google_Client library I need to mock it for the tests, the problem is that I can not mock the class correctly and it always ends up executing the original class so my tests with dependencies to that class fail

This is my class:

<?php
namespace App\Utils;

class Something
{
    public function doSomething($var)
    {
        if (!$var) {
            return false;
        }
        return $var * 2;
    }
}

This class I use in several of my controllers and the test I'm doing it like this:

<?php
namespace Tests\Feature;

use App\Utils\Something;
use Tests\TestCase;

class UserTest extends TestCase
{

    protected function setUp()
    {
        parent::setUp();
        $mock = \Mockery::mock(Something::class);
        $mock->shouldReceive('doSomething')->with(1)->andReturn(10);
        $this->app->instance(Something::class, $mock);
    }

    public function testExample()
    {
        $response = $this->json('post', '/api/tests', ["number":1]);

        $response->assertExactJson(["result":10]);
    }

}

But when executing the test it fails because the answer it gives is ["result": 3], meaning that the mock is not being applied ...
I do not know if I am doing my mock or what is happening but I can not find how to make it work.

Thanks in advance

    
asked by Paris N. Salguero 09.03.2018 в 23:34
source

0 answers