function used by __construct($val) not being mocked

Published 1 week ago by techguy92

I'm new to Testing and mocking. I need help on how to mock particular functions in my __construct class for one of my objects. These functions pull data from a database and return values to populate object properties.

I'm currently using Laravel 5.5

Here is what I have

namespace Tests\Unit;

use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Mockery;
use App\Event;
use Test\TestCase;
use stdClass;

class EventTest extends TestCase
{
    const TEST_EVENT_ID = 2;

public function testInstanceEventWithInteger()
{
$testResponse = (object)[
            'name' => 'TEST Event',
            'description' => 'Test Event',
            'beginDate' => '2018-05-04',
            'orgCode' => '014',
        ];

        $event = Mockery::mock(Event::class, [self::TEST_EVENT_ID])->makePartial();
        $event->shouldAllowMockingProtectedMethods();
        $event->shouldReceive('getCampusName')
            ->with('014')
            ->atLeast(1)
            ->andReturn('LHS Harmon');
        $event->shouldReceive('getEventInformation')
            ->with(self::TEST_EVENT_ID)
            ->atLeast(1)
            ->andReturn($testObject);
    
    $this->assertSame($event->name, 'TEST Event');
}

But instead of mocking the functions, I get the data from the database and not the data from my $testResponse. Am I doing this all wrong or what am I missing to get this to retrieve the data from the mocked functions instead of the pulling from the actual database

Best Answer (As Selected By techguy92)
techguy92

I'm going to make a test Object that extends the original object to test the other functions, by prepopulate the origin data.

techguy92

I'm going to make a test Object that extends the original object to test the other functions, by prepopulate the origin data.

Please sign in or create an account to participate in this conversation.