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

查看:143
本文介绍了如何在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.

推荐答案

您例如

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 doc表示可以这样做

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