go-sqlmock icon indicating copy to clipboard operation
go-sqlmock copied to clipboard

Include Any{Type} to package.

Open edwinthinks opened this issue 8 years ago • 4 comments
trafficstars

Hey,

I was wondering if it would be beneficial to add AnyTime to the library so that I don't need to define it in my test code.

I would also wonder if a AnyString would be useful. I had to create something like that to handle mocking the creation of a User model with a password that need to be encrypted via bcrypt before storing into the database. AnyString is useful because bcrypt doesn't always output the same thing every time and makes matching troublesome.

Thanks again for this library!

edwinthinks avatar Mar 27 '17 15:03 edwinthinks

Hi, AnyArg was added only for corner cases like time.Time or other arguments, which simply cannot be generated for assertion. If you start to build your tests with Any*Types your tests will not be tests anymore. I do not encourage such library usage as I've seen many false tests in java using mockito - any() just because developers start to overuse such behavior for convenience and not correctness. I'm starting to regret that I've introduced AnyArg since it brings confusion now.

l3pp4rd avatar Mar 27 '17 17:03 l3pp4rd

Haven't read it well the first time. It would have been better to add AnyTime instead of AnyArg in the beginning. AnyTime is considerable, but I doubt that it would do more good than evil, since now I receive like the third enhancement request to add more any types, like Json Argument, to match it in various ways. I think all this type related stuff should be on behalf of users, since various SQL databases has various types. sqlmock currently provides ways to customize argument matching and in GO it is not so uncommon practice to just copy some code which just worked well in the previous project, especially when there is no generics available - and that is not a bad thing.

I will leave this open for now and see whether there are many users having the same inconveniences.

l3pp4rd avatar Mar 27 '17 18:03 l3pp4rd

Maybe, first of all a Wiki page could be created with some code samples for such arguments and then referenced in the documentation. And only later considered whether worth moving into the library

l3pp4rd avatar Mar 27 '17 18:03 l3pp4rd

@l3pp4rd sounds good to me. I agree that an overuse of the Any*Types could be encouraging the wrong sort of behavior when building tests.

edwinthinks avatar Mar 27 '17 20:03 edwinthinks