Rails is a great framework, isn’t it? It’s ships with all those great tools witch are supposed to help you automatically test your application. So you write specs, you practice TDD, BDD or maybe DDD, but over time you may notice something – it’s getting painfully slow. You app grows, your test suite grows and suddenly testing isn’t anymore so much fun as it supposed to be. You feel like it’s slowing you down.
So you you start using spork, you setup all the guards – but that are just a bandages for a bigger problem. And the problem is – Rails is just slow. I mean not like it’s a slow in production – when you just need to boot up your app once and that’s it – it’s not such a big deal – but you do it constantly in development, and more gems you add (because gems are cool, right?) it takes more and more time. Sooner or later you will start skipping on your tests and you may end up in very bad place.
What I started to do lately with my legacy app I was talking before is – I started testing outside rails. Rails is slow, so let’s take everything you can outside rails and work with pure ruby and rspec. What I mean is you should try and extract some logic into separate classes/modules and test them without database and without framework. And you just need a place to start, let me give you a very simple example where I started.
I have heavily customized two redcarpet render engines – basically I was interested in two helper methods in my ApplicationHelper module. But god how I was frustrated with loading times – I have there some very annoying tests, because I’m asserting html output of markdown input – as I needed absolute certainty everything worked as it supposed to the last tag.
So here’s what my spec look like, I added some comments for clarity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
And how fast is it? Pretty damn fast.
1 2 3 4 5 6 7
Now you don’t have to wait for rails to load just to test this one part of you app and you can do true TDD – your tests won’t slow you down anymore – they will drive you and your design. Of course you will still need to test models, controllers, do integrational tests, but I hope you see the point of testing without rails, and if you don’t – even so I encourage you to try it for yourself.