Installing Guard & Spork for Auto Running of Specs

Gemfile

Install the following gems with bundle install

Gemfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
.....
group :test_envs do
  gem 'rspec-rails', "~> 2.0"
  gem 'factory_girl_rails'
  gem 'faker'
  gem 'capybara', "~> 2.0.2"
  gem 'database_cleaner', "~> 0.9.1"
  gem 'launchy'

  # file system event triggers for ruby. See note at end about RVM 
  gem 'rb-fsevent', '0.9.3'

  # Notifications of test states, Requires Growl and growlnotify to be installed
  gem 'growl', '1.0.3'

  # Automated test running and speed imporvements
  gem 'guard-rspec'
  gem 'guard-spork', '1.2.0'
  gem 'childprocess', '0.3.6'# this was due to errors appearing in 0.3.7 and higher
  gem 'spork', '0.9.2'
  gem 'debugger'
end
.....

spec/spec_helper

Edit spec/spec_helper.rb to look below.

spec/spec_helper.rb
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require 'rubygems'
require 'spork'


ENV["RAILS_ENV"] = (ENV["RAILS_ENV"] == 'development' ? 'test' : ENV["RAILS_ENV"] + '_test')
require File.expand_path("../../config/environment", __FILE__)

require 'rspec/rails'
require 'rspec/autorun'
require "capybara/rspec"
#uncomment the following line to use spork with the debugger
# require 'spork/ext/ruby-debug'

Spork.prefork do
  require 'database_cleaner'

  # if defined? Capybara::Webkit
  #   Capybara.javascript_driver = :webkit
  # end

  Capybara.default_wait_time = 10

  Capybara.app_host = 'http://' + Settings.host.split(':')[0]
  Capybara.always_include_port = true

  # if defined? Delayed
  #   Delayed::Worker.delay_jobs = false
  # end

  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|

    config.include FactoryGirl::Syntax::Methods
    config.extend PaperclipMacros
    config.include FacebookMacros
    config.include FriendcareBootstrapMacros

    # ## Mock Framework
    #
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
    #
    # config.mock_with :mocha
    # config.mock_with :flexmock
    # config.mock_with :rr

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
    config.fixture_path = "#{::Rails.root}/spec/fixtures"

    # If true, the base class of anonymous controllers will be inferred
    # automatically. This will be the default behavior in future versions of
    # rspec-rails.
    config.infer_base_class_for_anonymous_controllers = false

    # Run specs in random order to surface order dependencies. If you find an
    # order dependency and want to debug it, you can fix the order by providing
    # the seed, which is printed after each run.
    #     --seed 1234
    config.order = "random"

    # If you're not using ActiveRecord, or you'd prefer not to run each of your
    # examples within a transaction, remove the following line or assign false
    # instead of true.
    config.use_transactional_fixtures = false

    # to make sure that each time the test suite is run it is done so against a new 
    # database i use the database_cleaner gem. so add that to your test group in the gem file
    # add set it up like so
    config.before(:suite) do
      DatabaseCleaner.strategy = :truncation
    end

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end
  end
end

Spork.each_run do

  if defined? QC
    QC::Conn.connection = ActiveRecord::Base.connection.raw_connection
  end
end

.rspec

.rspec
1
--format documentation --color --drb

Guardfile

Running run guard init rspec and guard init spork will produce a Guardfile similar to the one below. Not the additions of cucumber: false, test_unit: false, :wait => 45 to the spork call as leaving them out without test unit or cucumber configured can result in errors.

We also make an addition to the rspec guard call :cli => '--drb' so that it runs through spoke using distributed ruby

Guardfile
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
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

require 'active_support/core_ext'

guard 'spork', :rspec_env => { 'RAILS_ENV' => 'sexyjordan_test' }, cucumber: false,  test_unit: false, :wait => 45 do
  watch('config/application.rb')
  watch('config/environment.rb')
  watch(%r{^config/environments/.+\.rb$})
  watch(%r{^config/initializers/.+\.rb$})
  watch('Gemfile')
  watch('Gemfile.lock')
  watch('spec/spec_helper.rb')
  # watch('test/test_helper.rb')
  watch('spec/support/')
end

guard 'rspec', :all_after_pass => false, :cli => '--drb' do
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
  watch('spec/spec_helper.rb')  { "spec" }

  # Rails example
  watch(%r{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^app/(.*)(\.erb|\.haml)$})                 { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
  watch(%r{^app/controllers/(.+)_(controller)\.rb$})  { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
  watch(%r{^spec/support/(.+)\.rb$})                  { "spec" }
  watch('config/routes.rb')                           { "spec/routing" }
  watch('app/controllers/application_controller.rb')  { "spec/controllers" }

  watch(%r{^app/models/(.+)\.rb$}) {|m| "spec/models/#{m[1]}_spec.rb" }
  watch(%r{^app/services/(.+)\.rb$}) {|m| "spec/services/#{m[1]}_spec.rb" }

  # Capybara features specs
  watch(%r{^app/views/(.+)/.*\.(erb|haml)$})          { |m| "spec/features/#{m[1]}_spec.rb" }


end

Testing with queue classic

Using queue classic, spork and rspec. If the QC setup shares the rails connection to the database we need to add this to the each run command in spork

1
2
3
4
5
Spork.each_run do
  if defined? QC
    QC::Conn.connection = ActiveRecord::Base.connection.raw_connection
  end
end

FactoryGirl Cleaner Syntax

Adding

1
2
3
RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
end

Means we can now call build, create, createlist, withouth FactoryGirl infront

Paperclip and factory girl together

1
2
3
4
5
FactoryGirl.define do
  factory :timeline_memory do |f|
     image { Rack::Test::UploadedFile.new(Rails.root.join('spec', 'assets', 'test_memory.jpg'), 'image/jpg') }
  end
end

Notes

Auto detection didnt work with rvm and 1.9.2 straight away. Ruby had to be recompiled with an additional flag to deal with file system events the way we want. The fix is taken from here

Using RVM

You can use RVM to build your Ruby with GNU readline support. First install the readline package with RVM:

$ rvm pkg install readline --verify-downloads 1

Then configure RVM to use the readline package by adding

ruby_configure_flags=--with-readline-dir="$rvm_path/usr"

to ~/.rvm/user/db. Finally you need to reinstall your Ruby of choice:

$ rvm reinstall 1.9.3