【问题标题】:Call to undefined method ExampleTest::assertStatus()调用未定义的方法 ExampleTest::assertStatus()
【发布时间】:2019-07-08 08:27:02
【问题描述】:

我正在使用 Lumen 构建 api,并且还想为此编写单元测试用例。但我面临的问题不是单一的断言方法正在工作。像assertStatus()assertNotFound()assertJson() 等。它们都给出了错误,因为 Call to undefined method ExampleTest::assertMethod()。下面是我的 ExampleTest 文件。

<?php

use Laravel\Lumen\Testing\DatabaseMigrations;
use Laravel\Lumen\Testing\DatabaseTransactions;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->get('/');

        $this->assertEquals(
            $this->app->version(), $this->response->getContent()
        );
    }

    /** @test */
    public function testExample2()
    {
        $response = $this->get('/');

        //getting error here
        $response->assertStatus(200);
    }
}

我第一次在 Lumen 中编写测试用例。请指导我完成此过程。

【问题讨论】:

  • 可以在顶部加use Illuminate\Foundation\Testing\TestCase吗?
  • 添加到顶部后显示“Illuminate\Foundation\Testing\TestCase”类未找到。
  • TestCase.php 文件已经存在于同一目录中。所以我认为没有必要通过使用来包含它。
  • 你能检查一下TestCase 类是从什么扩展而来的吗?要使这项工作TestCase 应该扩展Illuminate\Foundation\Testing\TestCase$response 然后必须是Illuminate\Foundation\Testing\TestResponse
  • 当然。它是从下面的这个类扩展而来的。 &lt;?php abstract class TestCase extends Laravel\Lumen\Testing\TestCase { /** * Creates the application. * * @return \Laravel\Lumen\Application */ public function createApplication() { return require __DIR__.'/../bootstrap/app.php'; } }

标签: laravel unit-testing lumen


【解决方案1】:

如果你想要一个更像 Laravel 的测试套件,你可以使用这个包: https://packagist.org/packages/albertcht/lumen-testing

然后你可以使用像 Laravel 这样的断言

【讨论】:

    【解决方案2】:

    如果你使用 Lumen 的 Laravel\Lumen\Testing\TestCase 与 Laravel 的默认 Illuminate\Foundation\Testing\TestCase,一些断言方法是不同的。

    如果你想为Illuminate\Foundation\Testing\TestCase 断言状态:

    public function testHomePage()
        {
            $response = $this->get('/');
    
            $response->assertStatus(200);
        }
    

    Laravel\Lumen\Testing\TestCase 也一样:

    public function testHomePage()
        {
            $response = $this->get('/');
    
            $this->assertEquals(200, $this->response->status());
        }
    

    Laravel Testing DocumentationLumen Testing Documentation

    【讨论】:

      猜你喜欢
      • 2020-03-23
      • 2018-03-22
      • 2015-12-07
      • 2012-07-27
      • 2019-10-25
      • 2018-07-11
      • 2013-10-23
      • 2012-02-17
      • 1970-01-01
      相关资源
      最近更新 更多