2016-08-23 136 views
4

基本上我必须为许多Laravel控制器编写测试,其中大多数是CRUD(读取,存储,更新),并且大部分逻辑放置在那些(继承代码,不是我的)中。Laravel CRUD控制器测试

我需要做的是从用户的角度自动进行测试。所以我需要打所有的端点,并测试一个真实的数据库,并检查是否一切正常。

我几乎没有测试经验,但从我收集的控制器应该用集成/验收测试进行测试。现在,我确实通过扩展Laravel的TestCase与测试读取方法很好,这里有一个例子:

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的存储方法的测试创建的令牌和删除,一旦测试完成,所以它可以再次执行。

我只是不能概念化所有,因为我没有真正做过很多测试。

回答

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('[email protected]', '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()); 

} 

} 

希望这会帮助你。