单元测试 Laravel 的 FormRequest [英] Unit Test Laravel's FormRequest

查看:22
本文介绍了单元测试 Laravel 的 FormRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对各种自定义 FormRequest 输入进行单元测试.我找到了以下解决方案:

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

  1. 建议使用 $this->call(...) 方法并使用预期值 (答案链接).这有点矫枉过正,因为它直接依赖于路由控制器.

  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.

Taylor 的测试,来自 Laravel 框架 tests/Foundation/FoundationFormRequestTest.php 中找到.那里做了很多模拟和开销.

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).

示例表单请求:

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',
    ];
}

期望的测试:

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

   // assertTrue, required
   // ...

   // assertFalse, between
   // ...
}

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

我如何单独测试每个字段的每个验证规则(assert)?

推荐答案

我在 Laracast 并添加了一些自定义组合.

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

代码

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

更新

有一种 e2e 方法可以解决相同的问题.您可以POST将要检查的数据发送到相关路由,然后查看响应是否包含会话错误.

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']);

这篇关于单元测试 Laravel 的 FormRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆