【问题标题】:Laravel CRUD controller testLaravel CRUD 控制器测试
【发布时间】:2016-12-30 14:26:16
【问题描述】:

基本上我必须为许多 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 的 store 方法,使用该用户的电子邮件测试 TokensController 的 store 方法,使用创建的令牌测试其他控制器并在测试完成,可以再次执行。

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

【问题讨论】:

    标签: php laravel testing mocking phpunit


    【解决方案1】:

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

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

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-03-12
      • 2019-12-25
      • 2016-01-16
      • 2020-08-12
      • 2018-03-09
      • 2013-05-12
      • 2019-06-19
      • 2014-09-13
      • 2021-12-11
      相关资源
      最近更新 更多