Laravel 5.1模拟``唯一''验证规则 [英] Laravel 5.1 mocking 'unique' validation rule

查看:56
本文介绍了Laravel 5.1模拟``唯一''验证规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在应用程序上使用唯一"规则进行验证,但是对于测试,我们通过使用存储库模式来模拟数据库层.有没有一种方法可以模拟unique规则,以便它不会尝试检查数据库?

We are using the 'unique' rule for validation on the application, but for tests we're mocking the database layer through the use of repository pattern. Is there a way to mock the unique rule so that it doesn't attempt to check the database?

我尝试了模拟Illuminate\Validation\Validator::validateUnique,它不起作用,但是由于该方法是受保护的.

I tried mocking Illuminate\Validation\Validator::validateUnique it doesn't work but since the method is protected.

推荐答案

唯一规则以及其他一些规则(例如 exists )使用状态验证器进行验证检查.默认情况下,使用 DatabasePresenceVerifier .它实现了由2种方法组成的 PresenceVerifierInterface .您可以模拟该验证程序的方法,并在测试期间使用它.您可以使用

unique rule, along with a few other rules (e.g. exists) uses presence verifier to do validation checks. By default DatabasePresenceVerifier is used. It implements PresenceVerifierInterface that consists of 2 methods. You could mock methods of that verifier and use it during tests. You can access the verifier with

\Validator::getPresenceVerifier()
\Validator::setPresenceVerifier()

这篇关于Laravel 5.1模拟``唯一''验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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