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

查看:81
本文介绍了单元测试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()
{
    // ...
}

如何对每个字段的每个验证规则分别进行单元测试(断言)?

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

推荐答案

我在

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天全站免登陆