duanhuokuang5280 2016-05-02 08:28 采纳率: 100%
浏览 64
已采纳

单元测试Laravel的FormRequest

I am trying to unit test various custom FormRequest inputs. I found solutions that:

  1. Suggest using the $this->call(…) method and assert the response with the expected value (link to answer). This is overkill, because it creates a direct dependency on Routing and Controllers.

  2. Taylor’s test, from the Laravel Framework found in tests/Foundation/FoundationFormRequestTest.php. There is a lot of mocking and overhead done there.

I am looking for a solution where I can unit test individual field inputs against the rules (independent of other fields in the same request).

Sample FormRequest:

public function rules()
{
    return [
        'first_name' => 'required|between:2,50|alpha',
        'last_name'  => 'required|between:2,50|alpha',
        'email'      => 'required|email|unique:users,email',
        'username'   => 'required|between:6,50|alpha_num|unique:users,username',
        'password'   => 'required|between:8,50|alpha_num|confirmed',
    ];
}

Desired Test:

public function testFirstNameField()
{
   // assertFalse, required
   // ...

   // assertTrue, required
   // ...

   // assertFalse, between
   // ...
}

public function testLastNameField()
{
    // ...
}

How can I unit test (assert) each validation rule of every field in isolation and individually?

  • 写回答

2条回答 默认 最新

  • drvntaomy06331839 2016-05-08 06:10
    关注

    I found a good solution on Laracast and added some customization to the mix.

    The Code

    public function setUp()
    {
        parent::setUp();
        $this->rules     = (new UserStoreRequest())->rules();
        $this->validator = $this->app['validator'];
    }
    
    /** @test */
    public function valid_first_name()
    {
        $this->assertTrue($this->validateField('first_name', 'jon'));
        $this->assertTrue($this->validateField('first_name', 'jo'));
        $this->assertFalse($this->validateField('first_name', 'j'));
        $this->assertFalse($this->validateField('first_name', ''));
        $this->assertFalse($this->validateField('first_name', '1'));
        $this->assertFalse($this->validateField('first_name', 'jon1'));
    }
    
    protected function getFieldValidator($field, $value)
    {
        return $this->validator->make(
            [$field => $value], 
            [$field => $this->rules[$field]]
        );
    }
    
    protected function validateField($field, $value)
    {
        return $this->getFieldValidator($field, $value)->passes();
    }
    

    Update

    There is an e2e approach to the same problem. You can POST the data to be checked to the route in question and then see if the response contains session errors.

    $response = $this->json('POST', 
        '/route_in_question', 
        ['first_name' => 'S']
    );
    $response->assertSessionHasErrors(['first_name');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分