BadMethodCallException:调用未定义的方法 App\Models\User::getFirstMedia()
BadMethodCallException: Call to undefined method App\Models\User::getFirstMedia()
我正在尝试做:
$this->assertFileExists($user->getFirstMedia()->getPath());
在我的测试中。但是当我 运行 它时,我得到这个错误:
BadMethodCallException: Call to undefined method App\Models\User::getFirstMedia()
.
我愿意:
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
我也是:
class AssortmentTest extends TestCase implements hasMedia
{
use RefreshDatabase;
use InteractsWithMedia;
据我所知,我正在使用正确的特征。我在这里做错了什么?
编辑:
我的测试:
public function testUserCanUploadFile()
{
$this->withoutExceptionHandling();
$user = $this->signIn();
Storage::fake('public'); //Mock a disk
$file = UploadedFile::fake()->image('test.jpg'); //Upload a fake image.
$assortmentAttributes = Assortment::factory()->raw(); // Use the assortment factory.
$assortmentAttributes['image_path'] = $file; // Add a additional field in the assortment factory.
$this->post(route('assortments.store'), $assortmentAttributes)->assertRedirect(); // Post the fields to the assortmentcontroller store method.
//Storage::disk('public')->assertExists($file->hashName()); // Check if the field exists.
$this->assertFileExists($user->getFirstMedia()->getPath());
}
我的存储方法控制器代码:
if ($request->hasFile('image')) {
$image = $request->file('image'); //request the file
$fileName = md5_file($image . microtime()) . '.' . $image->getClientOriginalExtension(); //use md5 for security reasons and get the extension.
$image->storeAs('', $fileName, 'public'); //store the file in the public folder disk.
}
if ($request->wantsJson()) {
return response([], 204);
}
您在 TestCase
中实施您的特征,这是不正确的。如果您正在访问您的用户媒体,您应该在 User.php
模型 class 上实现特征,它位于 app/User.php
或 app/Models/User.php
.
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class User extends Authenticatable implements HasMedia {
use InteractsWithMedia;
}
我正在尝试做:
$this->assertFileExists($user->getFirstMedia()->getPath());
在我的测试中。但是当我 运行 它时,我得到这个错误:
BadMethodCallException: Call to undefined method App\Models\User::getFirstMedia()
.
我愿意:
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
我也是:
class AssortmentTest extends TestCase implements hasMedia
{
use RefreshDatabase;
use InteractsWithMedia;
据我所知,我正在使用正确的特征。我在这里做错了什么?
编辑:
我的测试:
public function testUserCanUploadFile()
{
$this->withoutExceptionHandling();
$user = $this->signIn();
Storage::fake('public'); //Mock a disk
$file = UploadedFile::fake()->image('test.jpg'); //Upload a fake image.
$assortmentAttributes = Assortment::factory()->raw(); // Use the assortment factory.
$assortmentAttributes['image_path'] = $file; // Add a additional field in the assortment factory.
$this->post(route('assortments.store'), $assortmentAttributes)->assertRedirect(); // Post the fields to the assortmentcontroller store method.
//Storage::disk('public')->assertExists($file->hashName()); // Check if the field exists.
$this->assertFileExists($user->getFirstMedia()->getPath());
}
我的存储方法控制器代码:
if ($request->hasFile('image')) {
$image = $request->file('image'); //request the file
$fileName = md5_file($image . microtime()) . '.' . $image->getClientOriginalExtension(); //use md5 for security reasons and get the extension.
$image->storeAs('', $fileName, 'public'); //store the file in the public folder disk.
}
if ($request->wantsJson()) {
return response([], 204);
}
您在 TestCase
中实施您的特征,这是不正确的。如果您正在访问您的用户媒体,您应该在 User.php
模型 class 上实现特征,它位于 app/User.php
或 app/Models/User.php
.
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class User extends Authenticatable implements HasMedia {
use InteractsWithMedia;
}