BlueScrinn
BlueScrinn

Reputation: 1

Why I can't create Mock in Laravel

Last days I work on Unit Testing in PHP Laravel. I am very new in tests, but I generaly read about how they should be make... but in fact I can't write it properly. Still got some errors. Here is my class ( method ) I want to test ( mock ):

class ApiHelper
{
    private $model_locations;

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

    public function calcAllDistances(string $location)
    {
        $request_data = $this->validLatLong($location);
        if(!$request_data) {
            return null;
        }
        $user_lat = $request_data[0];
        $user_lng = $request_data[1];
        $all_locations = $this->model_locations::all()->toArray();
        $all_distances = [];

        foreach($all_locations as $single_location) {
            $point = new \stdClass;
            $point_lat = $single_location['lat'];
            $point_lng = $single_location['lng'];
            $distance = $this->calcDistance($user_lat,$user_lng,$point_lat,$point_lng);
            $point->name = $single_location['name'];
            $point->lat = $point_lat;
            $point->lng = $point_lng;
            $point->distance = $distance;
            array_push($all_distances,$point);
        }

        return $all_distances;
    }

We mock calcAllDistances() method.

Here is my test example;

public function testCalcAllDistances()
{
    $double = Mockery::mock(Locations::class)->shouldReceive('all')->with('')->andReturn(5)->getMock();
    $this->app->instance('App\Locations', $double);
    $object_under_tests = new \App\Helpers\ApiHelper($double);

    $result = $object_under_tests->calcAllDistances('21.132312,21.132312');

    $expected_result = [2.3, 4.7, 8.9];

    $this->assertEquals($expected_result, $result);
}

And no matter of what , I still got errors like :

1) Tests\Unit\ApiHelper::testCalcAllDistances
Mockery\Exception\BadMethodCallException: Static method Mockery_0_App_Locations::all() does not exist on this mock object

D:\xampp4\htdocs\api\api\app\Helpers\ApiHelper.php:26
D:\xampp4\htdocs\api\api\tests\Unit\ApiHelperTest.php:41

Caused by
Mockery\Exception\BadMethodCallException: Received Mockery_0_App_Locations::all(), but no expectations were specified

I swear with my soul, tried everything found in internet.... but I am still unable to write test. Basicly I want to mock Eloquent method all() to return given by me values, to make calcAllDistances() method work. Tried a lot, makePartial() etc but nothing helped me. Would be very thankful for help

Upvotes: 0

Views: 634

Answers (1)

PtrTon
PtrTon

Reputation: 3835

You are trying to mock a static method, but the code you wrote is mocking a public method instead. This answer contains the way of mocking you're looking for.

As a side note it's discouraged to write unit tests for Eloquent models, due to issues like mocking static methods. Laravel recommends writing database tests instead. These will seed a temporary database with the models you need for your test (at the cost of some performance).

Upvotes: 1

Related Questions