如何在 Laravel 5 中测试表单请求规则? [英] How to test form request rules in Laravel 5?

查看:19
本文介绍了如何在 Laravel 5 中测试表单请求规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个表单请求类并定义了一堆规则.现在我想测试这些规则,看看行为是否符合我们的预期.

I created a form request class and defined a bunch of rules. Now I would like to test these rules to see if the behaviour meets our expectations.

我如何编写测试来实现这一点?

How could I write a test to accomplish that?

非常感谢您的回答!

更新:更准确地说,我想编写一个单元测试来检查例如格式错误的电子邮件是否通过验证.问题是我不知道如何使用假输入创建请求的新实例.

Update: more precisely, I would like to write a unit test that would check e.g. if a badly formatted email passes validation or not. The problem is that I don't know how to create a new instance of the Request with fake input in it.

推荐答案

你需要在controller函数中有你的表单请求类,例如

You need to have your form request class in the controller function, for example

public function store(MyRequest $request)

现在创建 HTML 表单并尝试用不同的值填充它.如果验证失败,您将在会话中收到消息,如果验证成功,您将进入控制器功能.

Now create HTML form and try to fill it with different values. If validation fails then you will get messages in session, if it succeeds then you get into the controller function.

当单元测试然后调用 url 并将测试值添加为数组.Laravel 文档说可以这样做

When Unit testing then call the url and add the values for testing as array. Laravel doc says it can be done as

$response = $this->call($method, $uri, $parameters, $cookies, $files, $server, $content);

这篇关于如何在 Laravel 5 中测试表单请求规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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