Skip to content

Instantly share code, notes, and snippets.

@arikfr
Created August 1, 2011 14:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arikfr/1118210 to your computer and use it in GitHub Desktop.
Save arikfr/1118210 to your computer and use it in GitHub Desktop.
Guard/Spork talk

###The Problem

$ time rails runner "puts 1"
1

real	0m14.917s
user	0m10.923s
sys	0m3.007s[/code]
$ time rspec ./spec/models/asset_spec.rb

Finished in 0.02351 seconds

real	0m19.014s
user	0m13.108s
sys	0m3.440s

... so you wait 14seconds for Rails to boot, to run a 0.02 seconds spec. Now try to do it every few minutes.

###The Solution

Guard: Guard is a command line tool to easily handle events on files modifications (FSEvent / Inotify / Polling support). https://github.com/guard/guard

Guard-Spork: https://github.com/guard/guard-spork - reloads Spork on code changes that are cached (like lib/, initializers, etc) Guard-RSpec: https://github.com/guard/guard-rspec - runs specs on code changes (re runs until green, Growl alerts)

More guards: https://github.com/guard/guard/wiki/List-of-available-Guards

$ guard

So now the 19 seconds are cut down to: ...

$ time rspec ./spec/models/asset_spec.rb

Finished in 0.01704 seconds

real	0m3.046s
user	0m0.400s
sys	0m0.116s

Why 3 seconds? Because there are still some things we reload on every run of specs (although this can be probably optimized).

Besides this, you also save time by not having to run specs manually. So guard-rspec is relevant even if you don't have a big Rails project or non Rails project at all.

###More info

Spork reloading has its issues (that's the main reason we use Guard along with it). Some useful links that will help you out:

https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu

http://www.rubyinside.com/how-to-rails-3-and-rspec-2-4336.html

http://flux88.com/2011/04/using-guard-spork-with-mongoid-devise/

http://stackoverflow.com/questions/5913255/spork-and-cache-classes-problem-with-rspec-factory-girl-and-datamapper

http://blog.carbonfive.com/2010/12/10/speedy-test-iterations-for-rails-3-with-spork-and-guard/

guard/guard-spork#4

http://www.deploymentzone.com/2011/05/24/guard-rspec-2-and-growl/

https://gist.github.com/1071387 - My Rails 3 template incorporates many of the tips above and some more. Although a bit outdates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment