Laravel 5.1 mocking 'unique' validation rule

安稳与你 提交于 2019-12-24 15:14:19

问题


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?

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


回答1:


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


来源:https://stackoverflow.com/questions/34600605/laravel-5-1-mocking-unique-validation-rule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!