无法断言实际尺寸2与预期尺寸1相匹配

时间:2020-08-23 09:32:40

标签: laravel-7 laravel-testing

我有用于图书管理测试的功能测试

class BookManagemantTest extends TestCase
{
    use RefreshDatabase;

    /** @test */
    public function a_book_can_added_to_the_library()
    {
        //$this->withoutExceptionHandling();

        $response = $this->post('/book', [
            'title' => 'Book title',
            'author' => 'faraz',
        ]);

        $response->assertOk();

        $this->assertCount(1, Book::all());

        return Book::all();

    }

    /** @test */
    public function a_book_can_be_updated()
    {
        $book = $this->a_book_can_added_to_the_library()->first();

        $response = $this->patch('/book/' . $book->id, [
            'title' => 'new title',
            'author' => 'new author',
        ]);

        $response->assertOk();

        $this->assertEquals('new title', Book::first()->title);
        $this->assertEquals('new author', Book::first()->author);
    }
}

如果我测试仅测试a_book_can_be_updated()方法..我将通过测试。 (PHP供应商/ phpunit / phpunit / phpunit --filter a_book_can_be_updated)

但是,如果我测试了全班考试,则会收到此错误。

  1. Tests \ Feature \ BookManagemantTest :: a_book_can_be_updated 无法断言实际尺寸2与预期尺寸1相符。
我认为

控制器工作很好:

<?php

namespace App\Http\Controllers;

use App\Book;
use Illuminate\Http\Request;

class BookController extends Controller
{
    public function store(Request $request)
    {
        Book::create($this->validate_request($request));
    }

    public function update(Request $request,Book $book)
    {
        $book->update($this->validate_request($request));
    }

    public function  validate_request(Request $request)
    {
        return $request->validate([
            'title' => 'required',
            'author' => 'required',
        ]);
    }
}

0 个答案:

没有答案