Post APvaoEkbcqwbR3gCP2 by solnic@ruby.social
 (DIR) More posts by solnic@ruby.social
 (DIR) Post #APva6HOORV1qt3KTxI by slimdave@ruby.social
       2022-11-24T09:12:07Z
       
       0 likes, 1 repeats
       
       Suppose you want a test in #Rails #ActiveRecord that says "make sure this scope has applied". How about using `#annotate` in the scope definition, then testing the generated SQL for the presence of the annotation text?
       
 (DIR) Post #APva6HmqyYaw6um156 by james@ruby.social
       2022-11-24T11:48:15Z
       
       0 likes, 0 repeats
       
       @slimdave that's a neat approach!One thing to be cautious of is coupling the test with the implementation (e.g. "is this method called?"), rather than testing the actual behaviour ("does this ultimately perform the work/return the records I want?"), but that's a constant challenge in testing, and there are always tradeoffs either way.
       
 (DIR) Post #APvaoEkbcqwbR3gCP2 by solnic@ruby.social
       2022-11-24T11:56:13Z
       
       0 likes, 0 repeats
       
       @james @slimdave I was going to say the same thing. You're not testing much if you're just checking if SQL has some string in it. Cool trick nevertheless and TIL!
       
 (DIR) Post #APvgTKva41t6EkEKfI by slimdave@ruby.social
       2022-11-24T12:59:43Z
       
       0 likes, 0 repeats
       
       @james One common challenge with "does this ultimately perform the work/return the records I want?" (which I do have a lot of respect for and 99% of our tests do that) is that for anything non-trivial (for which people's definitions will vary) )I can usually look at a test and think of another 10 edge cases. The number of logical tests in a complex SQL statement can easily exceed my willingness to try to do that kind of testing.Shades of grey in here clearly, where different approaches suit.