#Http::fake() is ignored in the test

1 messages Β· Page 1 of 1 (latest)

tacit meadow
#

Hi,
I'm going trough Spatie's Laravel Testing course, and one of the chapters is covering Http::fake(). I'm trying to use the knowledge gained to implement testing in a small project of mine, but for some reason the Http::fake() is ignored. I'll try to explain what I've done;

  1. In the test I'm doing this:
it("shows content on the projects budget page", function () {
    Http::fake([        "https://api.harvestapp.com/v2/projects" => Http::response(""),
    ]);
    $this->get("/projects_budget")->assertSee("Kompetansebank");
});
  1. The "/projects_budget" in my route is calling [ProjectController::class, 'budget_index']

  2. The budget_index function in ProjectController can be simplified to this:

$this->harvest = new HarvestController();
$projectList = $this->harvest->projects();
  1. And in HarvestController this is the projects function:
public function projects(): array
    {
        $response = Http::withToken($this->token)
            ->withHeader("Harvest-Account-Id", $this->account_id)
            ->get("https://api.harvestapp.com/v2/projects?is_active=true&per_page=400");

        return $response->json();
    }

As you can see I'm using the Http function to fetch something from the Harvest API, but if I do a dump of $response in this function before returning it I get the actual content. I was hoping/expecting the Http::fake to override and let me shape the content for further testing.

Can anyone help me figure out what I'm missing? Thank you πŸ™

candid ferry
#

If you change your fake to just Http::fake(); it will catch your request. If you need to return something specific, you need to specify it, like

Http::fake([
        "*/projects_budget*" => Http::response('body', 200),
        "*" => Http::response()
    ]);

You can use wildcards to target the calls you wish

tacit meadow
candid ferry
#

Run php artisan route:list to see what routes you actually have.

tacit meadow
#

These are the routes.

#

It is just strange. All the examples I can find (and the course) are similar to your suggestions, but it has zero effect for some reason.
(For the record: I ran php artisan optimize:clear just to be on the safe side, but did not change anything)

candid ferry
#

This is weird, I'm not sure why you are new-ing up a controller?
$this->harvest = new HarvestController();
Sould it be $this->harvest = new Harvest(); ?

candid ferry
tacit meadow
#

It is basically just me trying to not have Controllers with too much code. I had everything in the ProjectController in the beginning, so more like a practise.
Not even sure if was correct to put the Harvest API calls in a seperate controller πŸ™ˆ Are they actions? resources? model-ish? But that is another story.

candid ferry
#

You are definitely using the use Illuminate\Support\Facades\Http?

tacit meadow
#

100% sure πŸ™‚

#

Removed the account_id / token to avoid sharing to much πŸ™‚

candid ferry
#

How about the test :


it("shows content on the projects budget page", function () {
    Http::fake();
    $this->get(route('projects_budget'))->assertSee("Kompetansebank");
});

What is the result?

tacit meadow
#

Still good. This is where I was hoping it not to be ok if I do a Http::fake(). It should not have been able to assertSee("Kompetansebank").

candid ferry
#

I would start by chucking dd() in the projects function and make sure I'm hitting that, work your way through the stacktrace. I'm supposed to be working, so might have to leave you to it.

tacit meadow
#

Sure thing. I want to thank you for trying to help me out. I should be off to bed as it's way past midnight, but I'll check in tomorrow. Maybe someone can see something we don't πŸ€·β€β™‚οΈ

tacit meadow
#

I'm petty sure I figured this out. Not sure what the problem was exactly, but what I found out is that if something fails, Pest/PHPUnit will not pick that up. It just continues as nothing has happened and give a positive result. So by dumping the get request in the test I was able to sort everything out.
I do find it strange though - if a get request returns a classic Laravel error page, then how come assertSee gives a positive result even though it does not find what it should πŸ€”