Laravel 5.5测试 - 调用未定义的方法:: see()

时间:2017-09-23 17:44:37

标签: laravel phpunit laravel-5.5

运行phpunit

时出现此错误

错误:调用未定义的方法测试\ Feature \ ViewConcertListingTest :: see()

这是我的代码:

类ViewConcertListingTest扩展了TestCase {     使用DatabaseMigrations;

/** @test */
public function user_can_view_a_concert_listing()
{
    // Arrange
    // Create a concert
    $concert = Concert::create([
        'title' => 'The Red Chord',
        'subtitle' => 'with Animosity and Lethargy',
        'date' => Carbon::parse('December 13, 2016 8:00pm'),
        'ticket_price' => 3250,
        'venue' => 'The Mosh Pit',
        'venue_address' => '123 Example Lane',
        'city' => 'Laraville',
        'state' => 'ON',
        'zip' => '17916',
        'additional_information' => 'For tickets, call (555) 555-5555'
    ]);

    // Act
    // View the concert listing
    $this->get('/concerts/' . $concert->id);

    // Assert
    // See the concert details
    $this->see('The Red Chord');
    $this->see('with Animosity and Lethargy');
    $this->see('December 13, 2016');
    $this->see('8:00pm');
    $this->see('32.50');
    $this->see('The Mosh Pit');
    $this->see('123 Example Lane');
    $this->see('Laraville, ON 17916');
    $this->see('For tickets, call (555) 555-5555');
}

}

有任何帮助吗? 谢谢!

3 个答案:

答案 0 :(得分:7)

如果其他人遇到此错误且问题代码看起来很熟悉,则来自Test-Driven Laravel course from Adam Wathan(强烈推荐!)。

如果您在本课程的早期课程中学习但使用的是Laravel 5.5,则需要更新以下内容:

  1. 使用$this->get('...');
  2. 而不是$response = $this->get('...');
  3. 使用$this->see()
  4. 而不是$response->assertSee()

    Laravel已将HTTP测试层和帮助程序方法从5.3(在屏幕录像中使用的Laravel版本)更新为5.5。您的5.5功能规格应更新为以下内容:

    <?php
    
    class ViewConcertListingTest extends TestCase
    {
    
        use DatabaseMigrations;
    
        /** @test */
        public function user_can_view_a_concert_listing()
        {
            // Arrange
            // Create a concert
            $concert = Concert::create([
                'title' => 'The Red Chord',
                // ...
            ]);
    
            // Act
            // View the concert listing
            $response = $this->get('/concerts/' . $concert->id);
    
            // Assert
            // See the concert details
            $response->assertSee('The Red Chord');
            // ...
        }
    }
    

答案 1 :(得分:2)

您指的是Laravel 5.3中提供的测试方法吗?这些已在5.4中删除,并作为单独的包提供; https://github.com/laravel/browser-kit-testing

要安装它们,请使用composer:

composer require laravel/browser-kit-testing --dev

答案 2 :(得分:1)

在这种情况下,你需要使用Laravel Dusk:

所以你的断言如下:

$this->browse(function ($browser) use ($user) {
                $browser->visit('/concerts/' . $concert->id)
                ->assertSee('The Red Chord');
                ->assertSee('with Animosity and Lethargy');
                ->assertSee('December 13, 2016');
                ->assertSee('8:00pm');
                ->assertSee('32.50');
                ->assertSee('The Mosh Pit');
                ->assertSee('123 Example Lane');
                ->assertSee('Laraville, ON 17916');
                ->assertSee('For tickets, call (555) 555-5555');
            });

您必须包含命名空间:

use Tests\DuskTestCase;
use Laravel\Dusk\Chrome;