PHPUnit在返回404时应该是200
我以前从未编写过测试用例,我正在尝试为我编写的API编写测试用例 我正试图通过post请求呼叫一条路线,因此我使用以下命令:PHPUnit在返回404时应该是200,php,laravel,phpunit,laravel-5.2,Php,Laravel,Phpunit,Laravel 5.2,我以前从未编写过测试用例,我正在尝试为我编写的API编写测试用例 我正试图通过post请求呼叫一条路线,因此我使用以下命令: public function testRequestCorrectApiKey() { //Request with X-Authorization sending correct API Key $response = $this->call('POST', '/authenticate', ['email' =>
public function testRequestCorrectApiKey()
{
//Request with X-Authorization sending correct API Key
$response = $this->call('POST', '/authenticate', ['email' => 'testingphpunit@phpunittest.com', 'password' => 'phpunittest', [], [], ['X-Authorization' => '123workingapikey123']]);
$this->assertEquals(200, $response->status());
}
这总是会失败,并出现以下错误:
断言404与200匹配失败
这自然表明它正在将请求发送到错误的路径。如何确保它被发布到正确的URL,以及如何查看它试图到达的内容
我已尝试将.env
文件中的应用程序url更新为http://localhost/gm-api/public
和我的config/app.php
文件
我还更新了stockTestCase.php
:
protected $baseUrl = 'http://localhost/gm-api/public';
我哪里出错了?我绕过了这个问题,个人认为这是一个更好的方法,只需使用Guzzle运行测试
/**
* Send a request with the correct API Key
*/
public function testRequestCorrectApiKey()
{
$key = ApiKey::find(1);
//Request with X-Authorization sending correct API Key
$path = 'authenticate';
$client = new Client(['base_uri' => env('APP_URL', 'http://localhost/gm-api/public/')]);
$response = $client->request("POST", $path, ["email" => "testingphpunit@phpunittest.com", "password" => "phpunittest", "headers" => ["X-Authorization" => $key->key ]]);
$status_code = $response->getStatusCode();
$this->assertEquals(200, $status_code);
}
这样做的好处是,您只需使用APP\u URL
在.env
文件中设置基本URL,就可以了
在默认测试用例中更改
$baseUrl
后,我不确定为什么我无法让$this->call()
工作-我只能假设它仍然命中错误的URL。然而,使用Guzzle已经为我解决了这个问题,而且实际上也在测试服务器的配置——鉴于PHPUnit推出了自己的版本,然而,这是在测试当前的服务器环境。使用调试器逐步完成吗?@Gordon当我从命令行运行phpunit时,我该怎么做?好了:当实际代码正常工作但单元测试失败并且您开始质疑自己的存在时,这将非常有用;)您确定它应该在url中点击public
?是否protected$baseUrl=http://localhost';代码>不适合你?