【问题标题】:Laravel 5.5 Tests - Call to undefined method ::see()Laravel 5.5 测试 - 调用未定义的方法 ::see()
【发布时间】:2018-03-05 02:02:52
【问题描述】:

运行 phpunit 时出现此错误

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

这是我的代码:

类 ViewConcertListingTest 扩展了 TestCase { 使用数据库迁移;

/** @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');
}

}

有什么帮助吗? 谢谢!

【问题讨论】:

    标签: laravel phpunit laravel-5.5


    【解决方案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;
    

    【讨论】:

      【解决方案2】:

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

      要安装它们,请使用 composer:

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

      【讨论】:

        【解决方案3】:

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

        如果您正在学习本课程的前几节课,但使用的是 Laravel 5.5,则需要更新一些内容:

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

        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');
                // ...
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-02-16
          • 2018-09-29
          • 2018-02-16
          • 1970-01-01
          • 2013-10-23
          • 1970-01-01
          • 2018-04-06
          • 1970-01-01
          相关资源
          最近更新 更多