I am starting to learn TDD by experimenting in Laravel's HTTP test. Here's my test function:
public function testLoginUsingUserDeni() {
$response = $this->json('POST', '/api/v1/login', [
'email' => 'ramadhanrperdana@gmail.com',
'password' => 'secret'
]);
$response
->assertStatus(200)
->assertJSONStructure($this->loginSuccessJsonStructure);
return $response->original['token'];
}
/**
* @depends testLoginUsingUserDeni
*/
public function testGambarBaru($token) {
Storage::fake('gambar');
$response = $this->json('POST', '/api/gambar/baru', [
'token' => $token,
'gambar' => UploadedFile::fake()->image('evidence.jpg'),
'posisi' => 1
]);
Storage::disk('gambar')->assertExists('evidence.jpg');
$response
->assertStatus(200)
->assertJSONStructure($this->gambarJsonStructure);
}
But, after running the test I got error like this:
PHPUnit 5.7.11 by Sebastian Bergmann and contributors.
Runtime: PHP 7.0.13-0ubuntu0.16.04.1
Configuration: /home/kromatin/Projects/PJB REMBANG/web-apps/KawistaK3_web/phpunit.xml
..E....................................... 42 / 42 (100%)
Time: 2.94 seconds, Memory: 20.00MB
There was 1 error:
1) Tests\Feature\Api\GambarTest::testGambarBaru
BadMethodCallException: Call to undefined method League\Flysystem\Filesystem::fake
/home/kromatin/Projects/PJB REMBANG/web-apps/KawistaK3_web/vendor/league/flysystem/src/Plugin/PluggableTrait.php:86
/home/kromatin/Projects/PJB REMBANG/web-apps/KawistaK3_web/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php:475
/home/kromatin/Projects/PJB REMBANG/web-apps/KawistaK3_web/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php:328
/home/kromatin/Projects/PJB REMBANG/web-apps/KawistaK3_web/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:221
/home/kromatin/Projects/PJB REMBANG/web-apps/KawistaK3_web/tests/Feature/Api/GambarTest.php:65
ERRORS!
Tests: 42, Assertions: 313, Errors: 1.
Script phpunit --color=always --verbose handling the test event returned with error code 2
I got error when executing testGambarBaru
function, while other functions worked well. That error pointed to the line where I place Storage::fake('gambar');
.
The purpose of my test function is to ensure my file upload API works well. I followed Laravel's documentation about testing file upload from this doc: https://laravel.com/docs/5.4/http-tests#testing-file-uploads. But the result said there is no method called fake
in Storage Facade. I've done some search inside vendor directory to find any fake
method around Storage facades but I can't find it. I am using Laravel 5.4. Is there something I've missed?
via Surya Darma Putra