Thoughts grows up by feeding itself with its own words.

Rails CI: RSpec+Guard+Spork


RSpec, a testing framework in Ruby, can be used to write tests for Ruby on Rails, but also for pure Ruby projects.

Guard, as the name suggests, monitors all the files in the project, and when a file changes in the project, it will start the corresponding test script according to the configuration.

Spork, which can be thought of as a lazy loading service, automatically loads all the files in the Rails project and only reloads the changed part of the files when they change, thus reducing the time it takes to load the tests when they start and greatly shortening the test preparation time. Of course, with the arrival of Ruby 2.0, the need for this component may not be as great.

What the three of them do together is that when you push your code to some server running the Guard service, it will automatically run the appropriate test scripts and communicate the results to the developer in some predefined way. Further, with other components, it is possible to achieve CI (Continuous Integration); functionally, it is also possible to prioritize the launch of the last failed test cases.


This testing framework, easy to get started, is the more commonly chosen testing framework for Rails projects. Compared to the default UnitTest framework, RSpec is better in flexibility and code readability; it is also very suitable for use in BDD (behavior-driven development) development. In addition, RSpec community support is more complete, easy to ask questions, and find answers.

However, the purpose of this article is not to introduce RSpec, so I won't go into too much detail about it. For more information, you can refer to the RSpec official website.



Add gem 'guard-rspec' to the Gemfile and bundle install

In addition, we need some specific gems according to your operating system. Mac users may also want to install Growl and growlnotify first. To be clear, this is because it essentially calls the system's notification function.

# Test gems on Macintosh OS X          
gem 'rb-fsevent', '0.9.1', :require => false
gem 'growl', '1.0.3'                      

# Test gems on Linux          
gem 'rb-inotify', '0.8.8'
gem 'libnotify', '0.5.9'

# Test gems on Windows          
gem 'rb-fchange', '0.0.5'
gem 'rb-notifu', '0.0.4'
gem 'win32console', '1.3.0' 


$ bundle exec guard init rspec 


After initialization, a Guardfile file is generated in the root directory, which contains some configuration information, mainly "what tests to run after what file changes".


$ bundel exec gurad 

Then you can change any file and see if it runs the corresponding tests.


There are good and bad things about this. Preloading results in less test preparation time, but you may have to restart the service after some configuration file changes. But it improves the speed significantly, here is the time comparison for the same tests:


  real 0m3.743s
  user 0m3.388s
  sys 0m0.184s


  real 0m0.746s
  user 0m0.316s
  sys 0m0.024s

Spork can be combined with Guard, you can start Guard directly and he will go to run Spork service by himself.


gem 'guard-spork'
gem 'spork' 

Bootstrapping (configuration):

$ bundle exec spork --bootstrap 

After executing the above command, the spec/spec_helper.rb file will change Now you need to take the contents of this file and put it in the block of spork.prefork.

Spork.prefork do                                                           
  # ...


$ bundle exec spork                         
Using RSpec
Loading Spork.prefork block...
Spork is ready and listening on 8989! 

When running tests at this point, add the parameter -drb to the end to see the effect.

Guard with Spork

$ bundle exec guard init spork 

The guardfile will be modified after this command is executed, but we don't usually have to change anything. Then when the guard is started, spork will be started at the same time.

$ bundle exec guard
< Back