2017-09-23 50 views
0

當我運行PHPUnit的Laravel 5.5測試 - 調用未定義的方法::看到()

錯誤我得到這個錯誤:調用未定義的方法測試\功能\ ViewConcertListingTest ::看到()

這是我的代碼:

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

}

任何幫助嗎? 謝謝!

回答

0

您將需要使用Laravel黃昏這樣的場景:

所以你的斷言將是如下:

$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; 
0

是你指的是Laravel 5.3中可用的測試方法?這些在5.4中被刪除,並作爲一個單獨的包提供; https://github.com/laravel/browser-kit-testing

要安裝它們,使用作曲:(!強烈推薦)

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

如果其他人遇到這個錯誤和問題的代碼看起來很熟悉,這是從Test-Driven Laravel course from Adam Wathan

如果你在使用過程中的早期經驗教訓沿以下,但使用Laravel 5.5,你需要更新幾件事情:

  1. 相反$this->get('...');的,使用$response = $this->get('...');。使用$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'); 
     // ... 
    } 
} 
相關問題