Laravel 5 PHP单元只检查第一个测试
我在运行Laravel,在尝试开发自动化系统时,出现了一个问题。我有以下测试:Laravel 5 PHP单元只检查第一个测试,laravel-5,phpunit,Laravel 5,Phpunit,我在运行Laravel,在尝试开发自动化系统时,出现了一个问题。我有以下测试: class SubmitSurvivorTest extends TestCase { /** * A basic test example. * * @return void */ //Test for submit with missing parameters public function testEmptySubmit() {
class SubmitSurvivorTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
//Test for submit with missing parameters
public function testEmptySubmit()
{
$response = $this->get('/submit/survivor');
$response->assertSee('422');
}
//Test for submit with complete parameters
public function testCorrectSubmit()
{
$response = $this->get('/submit/survivor?name=Jhon+Doe&age=22&gender=m&latitude=0&longitude=0&water=6&food=4&medicine=9&ammo=4');
$response->assertSee('666');
}
}
第一个应该返回true,因为如果我转到/submit/survivor,我将得到
{"status":"422","message":"The request could not be delivered since it's missing one or more paramters"}
第二个应该返回false,因为我得到了
{"status":"204","message":"Created successfully"}
但第二个结果仍然是真的。无论我在第二个assertSee中输入的值是多少,运行phpunit总是给我0次失败。其他测试也是如此,在我看来,这只是验证第一种方法
我试着把它改成
public function testEmptySubmit()
{
$response = $this->get('/submit/survivor');
$response->assertSee('422');
$asd = $this->get('/submit/survivor?name=Jhon+Doe&age=22&gender=m&latitude=0&longitude=0&water=6&food=4&medicine=9&ammo=4');
$asd->assertSee('666');
}
仍然没有失败,但是如果我改变第一个的值,我仍然会失败
更改顺序似乎没有任何影响,我将testCorrectSubmit更改为testEmptySubmit上面的,但仍然无法工作
如果我在第二个中将其更改为assertDontSee,则会给出一个失败,即使它应该返回true,错误也太大,甚至无法放入我的终端,但这与
[A ton of lines with html,js and stuff i can't identify]
</script>\n
</body>\n
</html>\n
' does not contain "666".
C:\xampp\htdocs\mysurvivorapi\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:331
C:\xampp\htdocs\mysurvivorapi\tests\Feature\submitSurvivorTest.php:27
FAILURES!
Tests: 6, Assertions: 6, Failures: 1.
不返回错误,但如果我将“Jhon”更改为“666”,则即使它们都不在页面中,也会返回错误
根据文档,它应该是这样的
public function testCorrectSubmit()
{
$response = $this->get('/submit/survivor',[
'name' => 'Jhon Doe',
'age' => '22',
'gender' => 'm',
'latitude' => '0',
'longitude' => '0',
'water' => '6',
'food'=>'4',
'medicine' =>'9',
'ammo' => '4']);
$response->assertSeeText('Jhon');
}
它只起部分作用,但会返回
Failed asserting that '{"status":"422","message":"The request could not be delivered since it's missing one or more paramters"}' contains "Jhon".
这表明它正在发出请求,但没有发送GET参数
我在@Spacemudd中完成了步骤3之前的所有操作,因为我不知道如何执行步骤4,现在它正在发送GET参数,因为现在Jhon Doe出现在数据库中。但是现在phpunit又回来了
C:\xampp\htdocs\mysurvivorapi>php vendor/phpunit/phpunit/phpunit
PHPUnit 7.0.2 by Sebastian Bergmann and contributors.
.{"status":"204","message":"Created successfully"}
就是这样,没有失败没有好没有什么,但是如果我进入幸存者表,我可以看到Jhon Doe被插入,我想这是一个进步
结果证明它按预期工作。最后一个奇怪的行为是因为我有其他测试,这些测试和测试一样错误。删除它们解决了问题,现在我将正确地重写
结果证明我又错了,似乎即使有正确的结构,没有其他测试测试完整提交失败。我删除了所有的测试,使用php artisan make创建了一个新的测试:test submitSurvivorTest,如下所示
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class submitSurvivorTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testSubmitEmpty()
{
$route = route('submit.survivor');
$response = $this->get($route);
$response->assertSee('422');
}
public function testSubmitComplete()
{
$route = route('submit.survivor', [
'name' => 'Jhon Doe',
'age' => '22',
'gender' => 'm',
'latitude' => '0',
'longitude' => '0',
'water' => '6',
'food'=> '4',
'medicine' => '9',
'ammo' => '4',
]);
$response = $this->get($route);
$response->assertStatus(200);
}
}
所有的功能都直接进入浏览器,只是不能与自动测试一起使用
此时此刻,跑步又回来了
C:\xampp\htdocs\mysurvivorapi>php vendor/phpunit/phpunit/phpunit
PHPUnit 7.0.2 by Sebastian Bergmann and contributors.
..Array{"status":"204","message":"Created successfully"}
在我看来,通过使用--testdox html,它甚至在显示第一次测试的结果之前就停止了执行,这很好
使用--进程隔离运行似乎可以避免崩溃,并且测试执行得很好。我已将所有echo$响应更改为return,因为这样似乎更好。现在它验证emptySubmit,但不验证completeSubmit
通过删除exit(),现在测试运行时不带——进程隔离
,并返回
断言“”包含“204”失败
但是,通过使其直接在submit方法中而不是在日志中返回响应,这一问题得到了解决。现在它似乎工作正常,因为它现在返回基于验证的响应。我将开发其余的自动化测试,看看问题是否再次出现
完成测试时,确保您的larvel.log
没有显示任何异常
确保您正在调用的路由不在中间件auth
下,在这种情况下,您必须在调用路由之前对用户进行身份验证
$response=$this->actingAs($user)->get('/submit/survivor')代码>
其中,$user
可以是用户模型类的实例
考虑使用helper命令route()
来构建URL
在web.php
中,为路线添加一个名称,例如:
Route::name('survives.submit')->get('survives/submit','SurvivorsController@store');代码>
在您的测试中,它会变成:
$route = route('survivors.submit', [
'name' => 'Jhon Doe',
'age' => '22',
'gender' => 'm',
'latitude' => '0',
'longitude' => '0',
'water' => '6',
'food'=> '4',
'medicine' => '9',
'ammo' => '4',
]);
$response = $this->get($route);
使用dd()代码>在提交控制器/函数中,然后运行测试。这将帮助您确定问题的确切位置
例如,为了确保接收到正确的会话数据,您可以调用dd(request()->(“u令牌”)除外)代码>如果您愿意多看一点的话,我已经添加了一些inofit@MatheusD.Lima我被exit()弄糊涂了代码>在您的代码中。你能把它取下来测试一下吗。。你的测试?哪一个?都是吗?@MatheusD.Lima是的。我认为函数不应该有exit()代码>在末尾。函数结束后总是有代码运行。@MatheusD.Lima不要使用echo
,始终使用return
。Do返回$result代码>。
public function submit(Request $request){
//Check if the required variables where sent, and if no it'll return an error
$validator = Validator::make($request->all(), [
'name' => 'required',
'age' => 'required',
'gender' => 'required',
'latitude' => 'required',
'longitude' => 'required',
'water' => 'required',
'food' => 'required',
'medicine' => 'required',
'ammo' => 'required'
]);
if ($validator->fails()) {
$result = array('status' => '422', 'message' => 'The request could not be delivered since it\'s missing one or more paramters');
echo json_encode($result);
}
//Create a new survivor
$survivor = new Survivor;
$survivor->name = $request->input('name');
$survivor->age = $request->input('age');
$survivor->gender = $request->input('gender');
$survivor->latitude = $request->input('latitude');
$survivor->longitude = $request->input('longitude');
$survivor->water = $request->input('water');
$survivor->food = $request->input('food');
$survivor->medicine = $request->input('medicine');
$survivor->ammunition = $request->input('ammo');
//Save survivor
$survivor->save();
//Set the success message
$result = array('status' => '204', 'message' => 'Created successfully');
//Save the change into the logs and display the result
app( 'App\Http\Controllers\LogsController')->submitLog($survivor->id,$survivor->id,"register",$result);
exit();
}
C:\xampp\htdocs\mysurvivorapi>php vendor/phpunit/phpunit/phpunit
PHPUnit 7.0.2 by Sebastian Bergmann and contributors.
..Array{"status":"204","message":"Created successfully"}
C:\xampp\htdocs\mysurvivorapi\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:253
C:\xampp\htdocs\mysurvivorapi\tests\Feature\submitSurvivorTest.php:36
FAILURES!
Tests: 4, Assertions: 4, Failures: 1.
$route = route('survivors.submit', [
'name' => 'Jhon Doe',
'age' => '22',
'gender' => 'm',
'latitude' => '0',
'longitude' => '0',
'water' => '6',
'food'=> '4',
'medicine' => '9',
'ammo' => '4',
]);
$response = $this->get($route);