laravel / browser-kit-testing

Provides backwards compatibility for BrowserKit testing in the latest Laravel release.
MIT License
508 stars 75 forks source link

[SOLVED] Call to a member function selectLink() on null #119

Closed hughgrigg closed 4 years ago

hughgrigg commented 4 years ago

Description:

Getting an error from InteractsWithPages saying Call to a member function selectLink() on null when trying to run a browser kit test. Looks like crawler is null on the class at that point.

vagrant@homestead:~/projects/foobar$ phpunit --filter ContinueTest 
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 216 ms, Memory: 20.00 MB

There was 1 error:

1) Tests\Feature\Play\StartToMapTest::testStartToMap
Error: Call to a member function selectLink() on null

/home/vagrant/projects/foobar/vendor/laravel/browser-kit-testing/src/Concerns/InteractsWithPages.php:497
/home/vagrant/projects/foobar/tests/Feature/Play/ContinueTest.php:13

ERRORS!
Tests: 1, Assertions: 2, Errors: 1.

Steps To Reproduce:

Test looks like this:

<?php

namespace Tests\Feature;

use Tests\TestCase;

class ContinueTest extends TestCase
{
    public function testContinue()
    {
        $this->get('/')->assertResponseOk()->see('Continue');

        $this->click('Continue');
    }
}

The parent Tests\TestCase class looks like this:

<?php

namespace Tests;

use Laravel\BrowserKitTesting\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    public $baseUrl = 'http://localhost';
}
hughgrigg commented 4 years ago

The test should use visit() and not get():

<?php

namespace Tests\Feature;

use Tests\TestCase;

class ContinueTest extends TestCase
{
    public function testContinue()
    {
        $this->visit('/')->assertResponseOk()->see('Continue');

        $this->click('Continue');
    }
}
indapublic commented 4 years ago

What if I want to check redirect and click in one test?

visit doesn't see redirect, but click requires visit