Laravel 5 phpunit getContent返回空

Laravel 5 phpunit getContent返回空,laravel-5,phpunit,Laravel 5,Phpunit,我正在学习Laravel 5.4,对Laravel和PHPUnit来说仍然是新的。在遵循在线基础教程之后,一切都很好 运行phpunit时,此测试功能正常工作 public function testBasicExample() { $response = $this->call( 'GET' , '/welcome'); $this->assertTrue(strpos($response->getContent(), 'Laravel') !== fals

我正在学习Laravel 5.4,对Laravel和PHPUnit来说仍然是新的。在遵循在线基础教程之后,一切都很好

运行phpunit时,此测试功能正常工作

public function testBasicExample()
{
    $response = $this->call( 'GET' , '/welcome');

    $this->assertTrue(strpos($response->getContent(), 'Laravel') !== false);

}
当我尝试测试Api时,问题出现了

我采取的步骤

  • 为书籍创建Api路由

  • 以json形式从localhost/api/books返回来自用户talbe的所有用户/

  • 我在浏览器中打开链接,json被正确返回

  • 将json复制并粘贴到在线json验证器jsonlint中,它是有效的

  • 创建一个新的测试函数

  • 运行phpunit 200状态测试通过,但assertJson未通过

  • 我尝试对$response->getContent()执行var_dump,发现它返回空

  • 现在我无法获取api/book/的getContent()。有人知道有没有解决办法吗

    谢谢


    在调用api之前,请尝试使用工厂创建一些数据:

    e、 g:

    然后


    如果您在phpunit.xml上设置“:memory:”作为您的数据库,您将不再看到本地数据库中的任何数据,这就是为什么您应该改用factory的原因。

    我遇到了类似的问题,即curl调用在内容中返回正确的JSON响应,但是API调用在public$original中显示类对象信息,但是public$内容是{}。你找到解决办法了吗?
    public function index()
    {
      $users = DB::table('users')->get()->toJson();
      echo $users;
    }
    
    public function test_index_method_returns_all_books()
    {
        $response = $this->call( 'GET' , '/api/books/');
          $this->assertEquals(200, $response->getStatusCode()); 
    
        $data = json_decode($response->getContent(),true);
        $this->assertJson($data);
    }
    
    factory(\App\Books::class, 20)->create();
    
    $response = $this->call( 'GET' , '/api/books/');