2017-06-27 32 views
1

我收到錯誤,如下面失敗斷言1場比賽在laravel測試預計0

Found unexpected records in database table [customers] that matched attributes [{"id":89}]. Failed asserting that 1 matches expected 0.

CustomerTest.php:

public function testDestroyCustomer() 
{ 
     // $this->assertTrue(true); 
     $post = factory(Customer::class,1)->create(); 
     $this->delete(route('customer.destroy', $post->id)); 
     $this->assertResponseStatus(500); 
     $this->dontSeeInDatabase('customers', ['id' => $post->id]); 
} 

routes.php文件

Route::resource('customer', 'CustomerController', [ 
    'names' => [ 
     'index' => 'customer' 
    ] 
]); 

CustomerController中的銷燬功能:

public function destroy($id) 
    { 
     $delete = Customer::where('id',$id)->delete(); 
    } 

我該如何解決錯誤?任何建議,請。

+1

在哪裏查詢可能返回集合,試圖在where子句後第()鏈接,或者使用find()來代替。 – Wreigh

+0

@WreighChristianSantos:我已經嘗試了兩個仍然錯誤仍然存​​在。任何想法 ? – 06011991

+0

應該只爲索引聲明路由名稱嗎?我對此並不熟悉,因爲我不使用這種格式的名稱。無論如何,我認爲如果無法找到路線摧毀,它應該拋出404對嗎? – Wreigh

回答

0

我覺得你的路線徵集刪除應該是,

變化:

route('customer.destroy', $post->id);route('customer.destroy', ['id' => $post->id]);

+0

我認爲錯誤沒有出來,因爲您聲稱錯誤500發生。 – Wreigh

+0

現在沒有錯誤。但是記錄被插入到數據庫中。刪除不會發生。我有疑問會刪除記錄或將它只是檢查破壞功能是否正確? – 06011991

+0

我對Laravel的單元測試不太瞭解,但是你可以像這裏所說的那樣調用destroy路由:https://laracasts.com/discuss/channels/testing/howto-test-delete-method-in-laravel -51 – Wreigh