D
D
Denis2021-02-12 12:51:25
Laravel
Denis, 2021-02-12 12:51:25

Testing with Http::fake(); in Laravel 8?

Trying to understand how HTTP::fake works in tests.
This is how I'm trying to simulate a response with a test html document in a test.

public function testChecks()
$html = file_get_contents(__DIR__ . '/../fixtures/test.html');
$fake = Http::fake(['test.ru' => Http::response($html)]);
$response = $this->post(route('route', $data['id']));

and I expect that when the action reaches this point $document = new DiDom\Document($url->name, true);
public function checks(Request $request, $id)
    {
        $url = DB::table('urls')->find($id);
        try {
            $check = Http::get($url->name);
        } catch (\Exception $e) {
            flash("Error: {$e->getMessage()}")->error();
            return back();
        }
        $document = new DiDom\Document($url->name, true);
        dd($document);

$document will contain test Html, among other things, as if I made this request to a real site.
but it doesn’t even reach it, or rather it crashes on it. because dd($document); not output.
I just don't understand why? What am I doing wrong in fake? but I can't figure out what I don't understand? And so, with a real request to a real site, everything works out, only the test fails.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Bezlepkin, 2017-09-15
@iBezlepkin

Show what you have in $test

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question