问题
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