Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I like to think about what a test of `Model.where(:x => "y").where(:y => "z").limit(3)` is supposed to be testing. If the test is that the correct query gets generated, then a simple check to ensure that the `where` calls have been made is fine. If the test is that the query returns the correct data, then either a real database or something lighter weight but which handles queries in the same way needs to be used, since it is really a test of how the query is interpreted rather than what the query is.


I’m testing the message. So that the (in this case) `.where` was called and with the right parameters... I trust ActiveRecord with the rest, and I don’t want to hit the DB.

I can’t simply use `expect(...).to receive(:where)`... even if I only cared that some `.where` was called...


Exactly. The usual way to deal with this would be to move the `where(:x => "y").where(:y => "z")` logic to a scope on the Model and test the scope in a unit test by checking if it returns the records as intended.




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: