加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – Laravel CRUD控制器测试

发布时间:2020-12-14 19:37:31 所属栏目:大数据 来源:网络整理
导读:基本上我必须为许多Laravel控制器编写测试,其中大多数是CRUD(读取,存储,更新),并且大多数逻辑都放在那些内部(继承代码,而不是我的). 我需要做的是从用户的角度自动化测试.因此,我需要点击所有端点并针对真实数据库进行测试,并检查一切是否正常. 我几乎没有测
基本上我必须为许多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',$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 IlluminateFoundationTestingWithoutMiddleware;
use IlluminateFoundationTestingDatabaseMigrations;
use IlluminateFoundationTestingDatabaseTransactions;

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());

}

}

希望这会帮助你.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读