This might be a basic misunderstanding on my part. I have a bunch of logic in my app which collects sets of data from several tables and combines them into memory structures. I want to write tests for that logic.
It seems to me that fixtures, factory girl and similar tools build in-memory model instances. If I do activerecord calls, like Model.find(foo: 12) won’t those apply only against records that were saved?
In most cases, I agree with @mrbrdo’s opinion: prefer rpsec’s stub method. but as a rails programmer, I think you have to know both the “fixture” and the “stub”.
Fixtures, whatever the
yamlfile or thefactory girl data, will both save into database. see yourconfig/database.ymlfile for the location where they are. Actually this is useful when you want to make sure that there is ALWAYS some data in the DB during your test, such as an “admin user” with a fixed ID.Stubs, it’s faster than fixture since it won’t be saved into DB, and maybe very useful when you want to perform a test that can’t implemented by “Fixtures”.
So, I suggest that you try both of them in your real coding life, and choose either of them according to the real context.