Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

How To Mock Facade In Tests? #31

Open
mikefromhl opened this issue Sep 2, 2023 · 0 comments
Open

How To Mock Facade In Tests? #31

mikefromhl opened this issue Sep 2, 2023 · 0 comments

Comments

@mikefromhl
Copy link

Is there a way to mock the Zendesk facade?

I'm following Laravel's documentation on Mocking Facades in Laravel 10, but I can't seem to get it working with the latest version of this package.

My ultimate goal is to ensure through testing that the methods to create or read tickets, users, etc. are being called in my controllers and models without making an external call to Zendesk to actually create the ticket, user, etc.

Here's a simple example:

// ZendeskController.php

use Huddle\Zendesk\Facades\Zendesk;

...

// The method that handles the route from the test below
public function getUser($id)
{
    return Zendesk::users()->find($id);
}
// ZendeskControllerTest.php

use Huddle\Zendesk\Facades\Zendesk;

...

/** @test */
public function the_zendesk_user_can_be_found()
{
    Zendesk::spy();

    // This user object has a valid zendesk_user_id property
    $this->actingAs($this->user)
        ->get("/api/zendesk/users/{$this->user->zendesk_user_id}");

    Zendesk::shouldHaveReceived('users')->once();
}

Running the test results in:

Method users(<Any Arguments>) from Mockery_0_Huddle_Zendesk_Services_ZendeskService should be called at least 1 times but called 0 times.

But, the test passes if I remove the Zendesk::spy() and Zendesk::shouldHaveReceived() lines, and returns the user if I dump the request.

What am I doing wrong?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant