I have a very large Rails code base, and we use lots of factories (FactoryGirl). The problem is, an instance of factory girl creates a database transaction so when we run our entire test suite, it takes a very long time. However, if we used Rspec mocks correctly then the tests would run lightning fast because mocks are feather light -- no DB transaction is required.
My question: when do you mock roles, entities or objects and when do you use a valid instance of an object? If someone could provide an example, that would be great.
I have learned to break down when to use FactoryGirl or active record instances in your tests. They should be used in your model tests and in your controller tests.
Model tests -- one must integrate with the database. If you are creating User tests that interact with user instances, then you will need User factories. However, if you are collaborating with other models, ones that are not the User model, then I would create doubles and stub methods for these collaborators.
One can also be generous using factories with Controller tests. Mainly because the controller is an exterior layer of your back end ecosystem. To test it, you will need to integrate with a lot of Rails and parts of your models. I find that it is ok to use factories here.
For the application logic -- service, domain, policy, plain old ruby objects -- you should tests each domain in isolation. For the unit or object under test, feel free to initialize it but create doubles, mocks and stubs for its collaborators.