Few weeks ago “Ryan Bates”:https://github.com/ryanb created a “screencast”:http://railscasts.com/episodes/275-how-i-test about testing. He uses “Guard”:https://github.com/guard/guard to run specs. Guard watches changes in files and may run appropriate specs files, which is very convenient. However, I have a little different way to run specs.
I do not always like to run full spec file, very often I need only to run one spec. To accomplish it very easily, I use few functions in my emacs, thanks to “rinari”:https://github.com/eschulte/rinari mode “emacs-rails”:https://github.com/remvee/emacs-rails mode:
rails-spec:run-this-spec
- runs specs with current line as rspec line parameters. I use this command very very often (C-c C-c ,
shortcut)
rails-spec:run-current
- runs all spec in current spec file or specs for file I actually edit. It works exactly like Guard, but I use it only when I need to (C-c C-c z .
shortcut)
rails-spec:run-last
- this function runs lately ran spec. It is great for TDD/BDD workflow, because with this I can run only one spec while I work on implementation in corresponding file (C-c C-c z l
shortcut)
One of the most important things in TDD/BDD process is running specs after each change in code. Thus we run spec even hundreds times in (good) developer day. That is why specs should run very fast. We may say, that specs that run more than 10 seconds are distracting and may cause checking lastest rss/facebook/google+ news ;) Of course, I talk about single spec or single spec file, because it is normal that running entire test suite takes few minutes in large projects. But we run entire test suite before commit, or even we use CI.
“Spork”:https://github.com/timcharper/spork is a great tool for TDD/BDD. It is test server, it loads our app to memory and forks for spec you actually run. With this, spec runs immediately, without waiting for rails app bootstraping. It makes a big difference in specs speed. For example I did some benchmarks in Teambox project:
Single spec:
And with Spork: Whole spec file:
And single spec with spork:
Nice speed up! With spork, single spec lasts about 3 seconds. In other projects with longer bootstrap time, we save much more time!
To run rspec with spork, first run spork `spork`
and then run any rspec with `--drb`
option. Do not forget to add this option to spec runner in your editor. Before first run we have to bootstrap spork with command `spork --bootstrap`
1 I write spec
2 Run only this spec (rails-spec:run-this-spec
in emacs)
3 It does not pass.
4 I write the code to make test pass
5 I run last spec (rails-spec:run-last
in emacs)
6 If spec does not pass, back to point 4
7 Spec pass
8 Run spec for entire file (rails-spec:run-current
) or even for the whole project.
I am hope that this note will usefull for You. I encourage you to run your spec with spork and run them with your editor. It gives you much more fun in TDD/BDD!