如何测试Laravel PHPUnit测试post方法,该方法包含来自multiple select输入元素的值

mum43rcc  于 2023-05-01  发布在  PHP
关注(0)|答案(1)|浏览(87)

我在Laravel中运行了一个phpunit测试方法,它测试了需要从multiple select input元素中选择多个值的文章创建。但是当我在PHPUnit中测试它并运行测试时,它显示post方法中的标签值中存在某种类型的错误。我不确定什么样的解决方案可以解决这个问题,让laravel认识到标签的价值。
来自create-article.blade.php的片段

<!-- tags -->
<div class="my-2">
    <label for="tags">Tags</label>
    <select multiple size="5"
        class="block w-auto py-1 px-2 my-2 appearance-none border border-sky-400 focus:outline-none focus:ring focus:ring-sky-400"
        name="tags[]" id="tags">
        @foreach ($tags as $tag)
            <option value="{{ $tag->id }}">{{ $tag->title }}</option>
        @endforeach
    </select>
</div>
ArticleTest.php
public function test_create_article_post(): void
{
    $user = User::factory()->accepted()->create();

    Storage::fake('test');

    $file = UploadedFile::fake()->image('avatar.jpg')->size(100);

    $tag = Tag::create([
        'title' => 'Health',
        'slug' => Str::slug(random_int(1000000000, 9999999999) . 'Health'),
        'user_id' => $user->id,
    ]);

    $response = $this->actingAs($user)
        ->post(route('admin.dashboard.create-article'), [
            "title" => "Temp Article",
            "meta_description" => "Just a temporary article.",
            "description" => "<p>Just some temporary article.</p>",
            "image" => $file,
            "tags" => "['$tag->id']",
        ]);

    $response->assertStatus(302);

    $this->assertDatabaseHas('articles', [
        'title' => 'Temp Article',
    ]);
}
zdwk9cvp

zdwk9cvp1#

你该换衣服了

"['$tag->id']"

收件人:

[$tag->id]

它现在发送['$tag-〉id']作为你的值。不是$tag-〉id的数组

相关问题