krystian2160's avatar

Laravel - when hitting web route twice in one test (get request)

Earlier in the test I have

$response = $this->get(route('home'));
$response->assertSee($goal->name);

Then some action, other lines of code

And then

$response = $this->get(route('home'));
$response->assertDontSee($goal->name);

And the assertion fails. But it shouldn't. It works great! And when I remove earlier two lines of code, I mean:

$response = $this->get(route('home'));
$response->assertSee($goal->name);

Now tests green and everything is fine.

I don't know what this is about, but I guess it's because it hits route('home') twice in the same test. As view is cached? As it gets the same output as before despite the new get request?

I don't know. Tried many things, e.g. $this->refreshApplication(); with no green result. Tried also Artisan::call('view:clear');, still the same.

What to do? What is this about?

0 likes
5 replies
arukomp's avatar

Could you post the whole test? Cos it seems like you're doing something in-between so that you don't see the $goal->name in the result any more. Can't help much without knowing the rest of the logic

krystian2160's avatar
[$user, $goal] = $this->newUserWithNewGoal();
        $text = $this->uniqueText();
    
        $response = $this->get(route('home'));
        $response->assertSee($goal->name);
    
        $this->post(route('commit.store', $goal), [
            'text' => $text,
            'done' => 3,
        ]);
        
        $this->assertTrue($goal->fresh()->done == 3);
    
        $response = $this->get(route('home'));
        $response->assertDontSee($goal->name);
        $response->assertStatus(Response::HTTP_OK);
hjgarcia1's avatar

Do it fail because you 'see' the goal name in the view?

Why do you change the second to last assertion to

$response->assertDontSee($goal->fresh()->name);
krystian2160's avatar

Still. $response->assertDontSee($goal->fresh()->name);

It fails in this assertion. :)

krystian2160's avatar

As I said, everything is fine and gut if i remove these 2 lines

$response = $this->get(route('home'));
        $response->assertSee($goal->name);

As if loads the same view as before, where we can See but we have to not See :) and we doesn't see. It just somehow... sees this

Please or to participate in this conversation.