dstewart101's avatar

integration test problems - can i pass data for integration testing?

Hi there - I have the following test created..

public function test_can_search_and_find_a_gameworld()
    {
        $gameworld = factory(Gameworld::class)->create([
            'name' => 'DS Test 1'
        ]);


        $this->visit('/game/search')  // pass
                 ->see('Search For Game')  // pass
                 ->type('DS', '#gameworld_name') // pass
                 ->press('Search') // pass
                 ->seePageIs('/game/search') // pass
                 ->see('Game Search Results') // pass
                 ->see('DS Test 1');  // fails - no data is making it to the view...
    }

What I am trying to prove is that I can search for a game, and see it come back in the view. The test fails, but when I actually do this action in the web site by hand, it passes. Now, some would say, why persist with a test if the actual functionality is in place, but I want my test to work! I suspect because I am not "redirecting" to the /game/search url with the data. Somehow it is falling off and not making it to the the view.

My view looks like this:

@section('content')
    <h1>Game Search Results</h1>
    <table class='table'>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>League</th>
            <th>Start Date</th>
            <th>Owner</th>
            <th></th>
        </tr>
        @if(empty($gameworld))
            empty.
        @else 
            <tr>
                <td>$gameworld->id</td>
                <td>$gameworld->name</td>
            </tr>
        @endif

        
    </table>
    
@endsection

If I was to change my last assertion to

->see('empty'); 

then my test passes.

My question is ultimately - is there a way to test with data sent to the view? Thanks in advance.

0 likes
5 replies
zachleigh's avatar

Does the factory work as expected? What do you see if you dd $gameworld?

$gameworld = factory(Gameworld::class)->create([
    'name' => 'DS Test 1'
]);

dd($gameworld);
dstewart101's avatar

Hi - thanks for replying...

$gameworld comes back as expected:

Illuminate\Database\Eloquent\Collection {#691
  #items: array:1 [
    0 => App\Gameworld {#692
      #table: "gameworld"
      #fillable: array:4 [
        0 => "name"
        1 => "league_id"
        2 => "start_date"
        3 => "owner_id"
      ]
      #connection: null
      #primaryKey: "id"
      #keyType: "int"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:7 [
        "id" => "1"
        "name" => "DS Test 1"
        "league_id" => "1"
        "start_date" => "1972-07-09"
        "owner_id" => "1"
        "created_at" => "2016-10-10 19:51:14"
        "updated_at" => "2016-10-10 19:51:14"
      ]
      #original: array:7 [
        "id" => "1"
        "name" => "DS Test 1"
        "league_id" => "1"
        "start_date" => "1972-07-09"
        "owner_id" => "1"
        "created_at" => "2016-10-10 19:51:14"
        "updated_at" => "2016-10-10 19:51:14"
      ]
      #relations: []
      #hidden: []
      #visible: []
      #appends: []
      #guarded: array:1 [
        0 => "*"
      ]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
  ]
}

As I say, when I actually do this search in my application (with this test data actually in a db) it plays out correctly.

JoolsMcFly's avatar

I'd just use xdebug and follow what's going on in the search controller. That way you know if it's not finding data, or finding but not sending back, or not posting to the right route.

dstewart101's avatar

Hello again folks - just wanted to let you know I got this working. Thanks to all who looked at this with me. In the end I changed passing my data using a

->with('gameworlds', $gameworlds)

instead of a

compact()

I'm sure there is nothing wrong in the compact syntax so it's likely I fixed something else by happy accident.

Please or to participate in this conversation.