Closed JayDijkstra closed 3 years ago
The generic message you're seeing The given data was invalid
is just the default exception message of Laravel's ValidationException
and it doesn't reflect the actual error that triggered it. You can get the error messages off of the exception with $exception->errors()
which would give you the array of human-readable error messages you're after.
As far as the error in your tests goes, you've likely either got a faulty factory (that is re-using unique keys) or you're not cleaning up the database properly between tests which is leading to duplicate keys.
Hope this helps!
Hey dwight,
The $exception->errors()
is the extra information which I needed!
Thank you!!!
Problem in short:
Within most of my test methods, I am generating test-data via factory class ( Laravel 6 and lower). There are those moments that a test fails, because of a model containing 'invalid' data (validated by the Watson validator).
This failures mostly occur due to an model being validated as invalid. (e.g. a duplicate entry for a
unique
rule). No matter on what rule the watson validator return invalid, and fails the test.. the default message will always be:Differences Application and Testsuite?
I did try to extract extra debug information by logging the errors via the
Handler.php
class:This is not a real effective solution because I have to scroll between multiple lines of stacktraces before actually finding the validation errors (needle / haystack principle). Also this only works when a ValidationException is thrown within the application (on a Controller's store or update method). The TestSuite does not trigger the Handler;s
report()
orrender()
method, not even with$this->withExceptionHandling()
defined in a testmethod.I did also try to add two array's to the model ( I am making use of an Abstract method which has the ValidatingTrait implemented on it). On this way the children of the abstract class will automatically be triggering the Watson validation Events.
AbstractModel.php
User.php
These arrays will will not change the default message The given data was invalid., which still leaves me with questions.
It would really be great if there is a method, or implementation on which I can change Laravel's default error message with the error bag (or a combined string of all errors within the error bag) as how the QueryException works when trying to insert data in a test:
QueryException example:
I hope someone could help me out!!
Kind Regards.