【发布时间】:2018-11-01 13:34:10
【问题描述】:
i want to run phpunit test in controller for
adding some data in database and testing api of project both
PostAddTest 类
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class PostAddTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$api = "/post/add";
$request = [
'title' => "xyz form....",
'content' => 'post add by xyz user.'
];
$response = $this->postJson($api,$request);
info("daa : ".print_r($response->getContent(),true));
$this->assertTrue(true);
}
}
如果我使用 phpunit 运行则成功运行
vendor/phpunit/bin --filter testExample
PHPUnit 6.5.5 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 6.84 seconds, Memory: 28.00MB
OK (1 test, 1 assertion)
我成功了,但是
如果我使用控制器运行,那么我会收到这样的错误
在 null {"exception":"[object] 上调用成员函数 make() (Symfony\Component\Debug\Exception\FatalThrowableError(代码:0): 在 null 处调用成员函数 make() PostProject/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:335
主控制器
public function index() {
(new PostAddTest)->testExample()
}
【问题讨论】:
-
“使用控制器运行”是什么意思?你是通过浏览器手动打开@index方法吗?
-
通过浏览器在控制器@index方法中运行特定的phpunit测试
标签: php laravel unit-testing laravel-5 phpunit