Recently, I’ve noticed a lot of dislike for Spring in the Rails community. This seems to happen after a developer has wasted a long time trying to debug a problem, then discovers the behaviour was actually caused by Spring not auto-reloading a particular piece of code. They decide Spring isn’t worth the hassle, and stop using it.
For those who have consciously chosen not to use Spring (or any other preloader such as Zeus), I’m really curious to know how you practice TDD in Rails.
I know that RSpec 3 encourages separating out tests that rely on Rails from those that don’t, by selectively requiring rails_helper or spec_helper. So if the bulk of your code is decoupled from Rails, your tests will be fast, and you won’t need Spring.
But in a typical Rails app, there’s usually a lot of other code which does depend on Rails - validations, associations, scopes, callbacks, etc.
If it takes 10 - 20 seconds to start-up your Rails app every time a test is run, then it seems that would make TDD extremely painful to do.
How do you handle this? Do you skip TDD for these areas? Or do you tolerate the slow startup?
I’ve had pretty good luck with Spring and I like it quite a bit! However, I have had a few things that have given false positives because the environment hadn’t reloaded. I tend to get around this by running individual tests through spring when I’m TDDing a feature, but before committing when running the full suite I usually do a full bundle exec rspec for example. This should tease out any false assumptions I made about something working in isolation in a preloaded environment. But also, if I am getting a little bit of weirdness I just do spring stop then re-run the test. It doesn’t happen very often (maybe once a week?) and I’m 100% willing to put up with that trade off. YMMV.
This is a bit brute force, but I’ve also got an alias in my dotfiles called respring, which runs spring stop && spring start. If I’m doing a test or something on my development machine where it suspect spring is the cause of weirdness, I just run that to ease my mind.