2017-04-08 43 views
2

,所以我想在我的應用程序添加配置文件圖像特徵。我正在使用TDD方法來做到這一點。上傳個人資料圖片的測試顯示爲綠色。但是當我運行測試更新配置文件圖片時,它會給出錯誤。下面是代碼:錯誤而測試中的文件上傳laravel 5.4

控制器:

public function store(StoreAvatarRequest $request) 
{ 
    $path = $request->file('avatar')->store('avatars'); 

    BasicInformation::updateOrCreate([ 
     'user_id' => auth()->id(), 
    ], [ 
     'user_id' => auth()->id(), 
     'avatar' => $path, 
    ]); 

    $this->showAlerts('alert-success', 'Profile Image uploaded successfully'); 

    return redirect()->route('avatar.index'); 

} 

public function update(StoreAvatarRequest $request, $id) 
{ 
    $basicProfile = BasicInformation::find($id)->first(); 
    $oldAvatarPath = $basicProfile->avatar; 

    if ($basicProfile->user_id == auth()->id()) { 
     $path = $request->file('avatar')->store('avatars'); 

     $basicProfile->avatar = $path; 
     $basicProfile->update(); 

     Storage::delete($oldAvatarPath); 
     $this->showAlerts('alert-success', 'Profile Image Updated Successfully'); 

     return redirect()->route('avatar.index'); 
    } else { 
     // TODO :: Need to add logic here 
    } 
} 

測試用例:

public function can_update_profile_picture() 
{ 
    $this->actingAs($this->lawyer)->post(route('avatar.store'), [ 
     'avatar' => UploadedFile::fake()->image('avatar.jpg', 600, 600) 
    ]); 

    $oldImagePath = $this->lawyer->information->avatar; 

    $this->actingAs($this->lawyer)->put(route('avatar.update', ['id' => $this->lawyer->information->id]), [ 
     'avatar' => UploadedFile::fake()->image('avatar1.jpg', 600, 600) 
    ]) 
     ->assertRedirect(route('avatar.index')) 
     ->assertSessionHas('status', 'Profile Image Updated Successfully'); 

    Storage::disk('local')->assertExists($this->lawyer->information->avatar); 
    Storage::disk('local')->assertMissing($oldImagePath); 
} 

我收到以下錯誤,當我運行測試

PHPUnit 5.7.19 by Sebastian Bergmann and contributors. 

F                 1/
1 (100%) 

Time: 298 ms, Memory: 20.00MB 

There was 1 failure: 
1) Tests\Feature\Dashboard\Lawyer\Account\ProfileImageTest::can_update_profile_picture 
Unable to find a file at path [local/c5tjUUQDzU4iKHauJK68Z801I5iaYJ7e3cVQ5iA1.jpeg]. 
Failed asserting that false is true. 
+0

@AmrAly這是調試錯字。我已糾正它。但我得到同樣的錯誤。同樣對於這兩個斷言(assertMissing&assertExists)我收到錯誤。 「無法找到路徑下的文件」, –

+0

在與用戶模型的BasicInformation型號和hasOne關係定義也化身列。所以我用引用$ oldImagePath $這個 - > lawyer->信息 - >頭像 –

回答

0

這是一個配置問題,然後優先使用store()或storeAs()方法。

首先,你必須配置filesystems.php認識到你的測試Storage磁盤作爲fake()盤:

'testing' => [ 
     'driver' => 'local', 
     'root' => storage_path('framework/testing/disks/'), 
     'visibility' => 'public', 

使用設置允許您在phpunit.xml使用APP_ENV值因拖欠該磁盤進行測試了。

此外,我使用在我的控制器中使用storeAs(),這樣我就可以在我的測試中針對存儲在assertExists()方法中的文件名進行測試。

$request->file('avatar')->storeAs('avatar', $request->file('avatar')->getClientOriginalName()) 

Storage::disk('avatar')->assertExists('avatar.jpg'); 

這是對我的作品和文件在每次測試之前被刪除和清理是不是一個問題。你也可以做的hashName測試,並通過獲取responsed並使用baseName()以獲取新的文件的名稱使用存儲()方法。

+0

我能夠使用類似的方法找出這個問題。 首先我使用下面這行代碼初始化假存儲磁盤 'Storage :: fake('public');' 然後,我使用以下代碼行發送虛假文件。 '$ fakeImage = UploadedFile :: fake() - > image('avatar.jpg',600,600),' 然後我斷言如果文件存在使用下面的代碼行。 '存儲::盤( '公共') - > assertExists($ fakeImage);' –

+0

@PawanKumar - 哪裏是你的控制器寫入文件?如果你的控制器把它寫到'disk('public')'那麼這對你有用,但不是你認爲它起作用的原因。 – insitderp