Laravel CRUD控制器测试

时间:2016-08-23 16:39:58

标签: php laravel testing mocking phpunit

基本上我必须为许多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的存储方法,使用创建的令牌测试其他控制器并删除一次测试完成后,可以再次执行。

我无法概念化所有这些,因为我还没有真正做过多次测试。

1 个答案:

答案 0 :(得分:0)

这是使用令牌和用户数据进行测试的示例 -

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

}

}

希望这会对你有所帮助。