首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PhpUnit -预期状态200,但收到500

PhpUnit -预期状态200,但收到500
EN

Stack Overflow用户
提问于 2022-07-29 06:48:47
回答 1查看 60关注 0票数 0

我有hasMany教育背景和教育奖项的用户表。然后educational backgrounds hasMany教育奖

这是我的Testcase,当用户上传图像时,我的端点会收到它

代码语言:javascript
复制
public function testuploadUsersImageEducationalAwards()
    {
        Storage::fake('public');
        $photo = UploadedFile::fake()->create('photo.png')->size(25000);

        $data = [
            'photo'                     => $photo,
            'award'                     => $this->faker->word,
            'educational_background_id' => EducationalBackground::factory()->create()->id
        ];

        $this->withoutExceptionHandling();

        $response = $this->sendPostRequestToEndpoint($data, 200);

        $data['file_name'] = $response['file_name'];
        unset($data['photo']);

        $response->assertJson($data)
            ->assertJsonStructure([
                'id',
                'award',
                'photo',
                'educational_background_id',
                'created_at',
                'updated_at',
            ]);

        $this->assertDatabaseHas('users.educational_awards', $data);
    }

下面是我的断言状态为200的端点

代码语言:javascript
复制
private function sendPostRequestToEndpoint(array $data, $status)
    {
        return $this->json("POST", '/api/users/educational-award/upload-picture', $data)->assertStatus($status);
    }

更新

这是我的EducationalBackgroundFactory

代码语言:javascript
复制
class EducationalBackgroundFactory extends Factory
{
    protected $model = EducationalBackground::class;

    public function definition()
    {
        return [
            'user_id'           => User::factory()->create()->id,
            'studies_type'      => $this->faker->randomElement([EducationalBackground::BASIC, EducationalBackground::SECONDARY, EducationalBackground::UNDERGRADUATE, EducationalBackground::GRADUATESCHOOL]),
            'year'              => Carbon::now()->format("Y"),
            'course'            => $this->faker->word,
        ];
    }
}

这是我的EducationalBackground模型

代码语言:javascript
复制
class EducationalBackground extends Model
{
    use HasFactory;

    const BASIC = "basic";
    const SECONDARY = "secondary";
    const UNDERGRADUATE = "undergrad";
    const GRADUATESCHOOL = "grad";

    protected $table = 'users.educational_backgrounds';
    protected $fillable = [
        'user_id',
        'studies_type',
        'year',
        'course',
    ];

    public function user()
    {
        return $this->belongsTo('App\Models\User', 'user_id');
    }

    public function educationalAwards()
    {
        return $this->hasMany("App\Models\Users\EducationalAward", "educational_background_id");
    }
}

这是我的迁徙

代码语言:javascript
复制
public function up()
    {
        
        Schema::create('users.educational_backgrounds', function(Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users');
            $table->enum('studies_type', ['basic', 'secondary', 'undergrad', 'grad']);
            $table->integer('year');
            $table->string('course')->nullable();
            $table->timestamps();
        });

                
    }

这是我的控制器代码

代码语言:javascript
复制
public function uploadUsersImageEducationalAwards(UserImageRequest $request, EducationalBackground $educational_background)
    {
        $uploaded_image = $request->photo->store('users/educational_awards');
        $file_type = $request->photo->getClientOriginalExtension();

        $file = EducationalAward::create([
            'educational_background_id' => $educational_background->id,
            'award'                     => $request->award,
            'photo'                     => $uploaded_image,
        ]);
        return response()->json($file, 200);
    }

但是这给了我500的状态,我找到了一种详细记录错误的方法。为了更清晰起见,这里有一张图片

EN

回答 1

Stack Overflow用户

发布于 2022-07-29 08:27:35

在发送响应之前删除unset函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73162621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档