php – Laravel CRUD控制器测试
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – Laravel CRUD控制器测试,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3026字,纯文字阅读大概需要5分钟。
内容图文
![php – Laravel CRUD控制器测试](/upload/InfoBanner/zyjiaocheng/767/5e8759d6e5644d95bfbeb4eee718465f.jpg)
基本上我必须为许多Laravel控制器编写测试,其中大多数是CRUD(读取,存储,更新),并且大多数逻辑都放在那些内部(继承代码,而不是我的).
我需要做的是从用户的角度自动化测试.因此,我需要点击所有端点并针对真实数据库进行测试,并检查一切是否正常.
我几乎没有测试经验,但从我收集的控制器应该通过集成/验收测试进行测试.现在我通过扩展Laravel的TestCase测试Read方法做得很好,这是一个例子:
class SongsTest extends TestCase
{
public function testBasicIndex()
{
$arguments = [];
$response = $this->call('GET', '/songs', $arguments);
$this->assertResponseOk();
$this->seeJson();
}
/**
* @dataProvider providerSearchQuery
*/
public function testSearchIndex($query)
{
$arguments = ['srquery' => $query];
$response = $this->call('GET', '/songs', $arguments);
$this->assertResponseOk();
$this->seeJson();
}
public function providerSearchQuery()
{
return array(
array('a'),
array('as%+='),
array('test?Someqsdag8hj$%$')
);
}
public function testGetSongsById()
{
$id = 1;
$response = $this->call('GET', '/songs/' . $id);
$this->assertContains($response->getStatusCode(), [200, 404]);
$this->seeJson();
if($response->getStatusCode() == 404)
{
$content = json_decode($response->getContent());
$this->assertContains($content->message[0], ['Song not found', 'Item not active']);
}
}
}
这些测试命中了端点并检查响应是否为200,格式是JSON还是其他一些东西.这些工作正常.
我遇到的问题是:
比方说,我们有一个UserController和一个创建用户的方法.之后,应该在TokensController中使用所述用户来创建一个令牌,该令牌应该以某种方式被记住并在将来使用令牌保护请求的测试中使用.
我的问题 :
我如何自动化:通过在测试数据库中创建真实用户(无需模拟)测试UserController的存储方法,使用该用户的电子邮件测试TokensController的存储方法,使用创建的令牌测试其他控制器并在测试完成后删除,所以它可以再次执行.
我无法概念化所有这些,因为我还没有真正做过多少测试.
解决方法:
这是使用令牌和用户数据进行测试的示例 –
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PostTest extends TestCase
{
use WithoutMiddleware;
public $token = "lrSFMf0DpqRAh4BXTXWHp5VgFTq4CqA68qY3jG2CqvcpNTT6m0y9Qs6OdpSn";
/*
A browser that receives a 302 response code HAS to redirect which means it will take the URL in the response and submit a new request. The result you see in your browser is the redirected page.
Unit testing does not redirect. Your unit test is only doing what you direct it to do. If your unit test should test for the redirect then you evaluate the response and the correct assertion is 302 and not 200.
*/
public function testExample()
{
$this->assertTrue(true);
}
public function testLogin()
{
$this->visit('/')
->type('abc@gmail.com', 'email')
->type('123456', 'password')
->press('Login') // type submit - value / button - lable
->seePageIs('/Wall'); // for redirect url
}
public function testFavourite()
{
$this->testLogin();
$request = [
'user_id' => '172',
'token' => $this->token,
'post_id' => '500'
];
$response = $this->call('POST', '/DoFavoriteDisc',$request);
$this->assertEquals(200, $response->getStatusCode());
}
}
希望这会帮助你.
内容总结
以上是互联网集市为您收集整理的php – Laravel CRUD控制器测试全部内容,希望文章能够帮你解决php – Laravel CRUD控制器测试所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。