0

I have the following test...

<?php

namespace Tests\Feature;

use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;

use App\Gameworld;
use Carbon\Carbon;

class VisitGameworldTest extends DuskTestCase
{

use DatabaseMigrations;

public function test_can_visit_gameworld()
{
    // arrange
    $gameworld = Gameworld::create([
      'name' => 'LMS Main Game',
      'start_date' => Carbon::parse('September 6th, 2017'),
      'league' => 'Premier League',
    ]);

    //dd($gameworld->id);

    $this->browse(function (Browser $browser) {
        $browser->visit('gameworld/'.$gameworld->id)
          ->assertSee('LMS Main Game')
          ->assertSee('September 6th, 2017')
          ->assertSee('Premier League');
    });
    }
}

The error I get is

1) Tests\Feature\VisitGameworldTest::test_can_visit_gameworld
ErrorException: Undefined variable: gameworld

When I do dd($gameworld) I get the result I expect.

Can someone tell me what has gone wrong here?

Thanks.

dstewart101
  • 1,084
  • 1
  • 16
  • 38
  • 1
    A anonymous function is a different scope so you need to tell it about the variable. You probably want the `use` keyword. See http://php.net/manual/en/functions.anonymous.php – Jonnix Sep 17 '17 at 21:34
  • argh! of course i do. many thanks. – dstewart101 Sep 17 '17 at 21:38

1 Answers1

2

It's out of scope

function (Browser $browser) use($gameworld)
Paras
  • 9,258
  • 31
  • 55