Is there a way to overwrite values of Http::fake([])
in Laravel during testing. I've noticed that if I set a value during a faker, eg. Http::fake(['url1.com' => Http::response('OK'), 'url2.com' => Http::response('Not Found', 404),])
, if for some reason I need to change the value of say url1.com
to something else such as ['message' => 'Success']
, if I "update" the value by calling Http::fake(['url1.com' => Http::response(['message' => 'Success'])
again at a later point, I'd expect the response when I call Http::get('url1.com')
to return ['message' => 'Success']
but it instead always returns OK
which was the original value set.
Same way if I later call Http::fake(['url2.com' => Http::response(['message' => 'Object found.'])])
, I would expect the response when I call Http::get('url2.com')
to be ['message' => 'Object found.']
but it'll always return Not found
which was the original value set.
You can use achieve this by swapping the Http client
Http::fake(['https://potato.com' => Http::response('Yummy!', 200)]);
dump(Http::get('https://potato.com')->body()); // Yummy!
at some point in your test, you can reset the Http Facade using Facade Swap
Http::swap(app(\Illuminate\Http\Client\Factory::class));
now you can change the fake value to whatever you want
Http::fake(['https://potato.com' => Http::response("That's what the dcotor ordered.", 200)]);
dump(Http::get('https://potato.com')->body()); // That's what the doctor orderd.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With