How to combine autotest and spork in Rails testing? - ruby-on-rails

Autotest increases the speed at which tests run by running only the changed tests.
But I want to push it even further by using spork to preload the Rails environment, so that I will get even faster feedback.
Is this possible?
Autotest : https://github.com/grosser/autotest
Spork : http://github.com/timcharper/spork

ARTICLE1 mikbe has you covered! I would attempt to rephrase it here, but the post does such a great job.
If you happen to be on OSX, there are also instructions to utilize Growl for tray notifications.
ARTICLE2 Ruby Inside also has a walkthrough for Rails 3 and RSpec 2.

If you use Ruby 1.9 and want to use spork and autotest together with Test::Unit (actually MiniTest) try this:
Gemfile:
group :test do
# Newer version of test::unit:
gem 'minitest'
# spork preloads a rails instance which is forked every time the tests are
# run, removing test startup time.
gem 'spork'
# Run 'spork minitest' to start drb server (test server). Use 'testdrb' to
# run individual tests via spork.
gem 'spork-minitest'
# Run 'bundle exec autotest' to rerun relevant tests whenever a file/test is
# changed. '.autotest' makes sure the tests are run via test server (spork).
gem 'autotest-standalone'
# -pure gives us autotest without ZenTest gem.
gem 'autotest-rails-pure'
end
.autotest:
class Autotest
# run tests over drb server (spork)
def make_test_cmd files_to_test
if files_to_test.empty?
"" # no tests to run
else
"testdrb #{files_to_test.keys.join(' ')}"
end
end
end
(Note: Instructions says bin/testdrb, but I changed it to testdrb to make it work for me.)
In a terminal:
spork minitest --bootstrap
Edit test/test_helper.rband follow instructions.
After the above setup is done once, you can start the test server:
spork minitest
Finally start autotest in another terminal:
bundle exec autotest
And (hopefully) enjoy really fast autotesting with MiniTest.

I haven't tried it yet, but there's a section in chapter 3 of the RailsTutorial that tells some "hacks" to set up spork. The tutorial currently says:
... as of this writing Spork doesn’t officially support Rails 3
The chapter goes on to tell how to set it up with autotest. One thing to know is that you'll need
--drb
in your .rspec file.

Related

Spork with Capybara on Windows 7

Here is my problem: I have started spork on my Windows 7 and it just works fine (2 magazine_slave_service are setup -- after several attempts though). But when I run bundle exec rspec spec to execute my RSpec tests on another console, it gives me this error message:
undefined method 'read_all' for nil:NilClass <NoMethodError>
It's in 1.8/gems/...../lib/spork/run_strategy/magazine.rb:89:in 'run'
P.S: When I run rake spec it just does what's expected, my problem shows up when I want to use spork to speed up my BDD.
I'm running spork on Win7-x86/Ruby1.8.7.
Any ideas?
And the answer is:
Add gem 'spork', '> 0.9.0.rc9' (Instead of gem 'spork', '~> 0.9.0.rc9')
Run "rspec spec/models" (for models)
Although, in general spork seems like doesn't work fine in windows ..sometimes it can't fork the processes properly. It works fine randomly (at least for me!)..You should try again and again to get it working.

Problems with Guard, Spork, Rspec & Rails 3

I've followed the spork railscast video and it gives me the following error when I try to run guard:
Guard is now watching at '/Users/m/work/'
Starting Spork for Test::Unit & RSpec
Couldn't find a supported test framework that begins with 'testunit'
Supported test frameworks:
( ) Cucumber
(*) RSpec
Legend: ( ) - not detected in project (*) - detected
Using RSpec
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
--> ERROR: Could not start Spork server for Test::Unit & RSpec. Make sure you can use it manually first.
Guard::RSpec is running, with RSpec 2!
Running all specs
It looks like The spork server starts up fine and then errors and tries to carry on. I've tried making the :wait option 120 seconds and it still has the same issue.
Spork works fine if I execute the tests without guard & guard-spork
Environment:
Mac OSX
rails (3.1.1)
guard (0.8.8)
guard-rspec (0.5.4)
spork (0.9.0.rc9)
guard-spork (0.3.1)
How would I go about debugging this issue? I have no idea where to start.
I had exactly this issue on Ubuntu. My solution was simple enough:
1) I stopped guard
2) I deleted the test folder
3) started guard
This time, instead of seeing Starting Spork for Test::Unit & RSpec, I got Starting Spork for RSpec. So spork automatically recognised that my test folder was no longer needed and everything ran sweetly.
Interestingly enough the
Supported test frameworks:
( ) Cucumber
(*) RSpec
message also disappeared and I got exactly the expected results as per railscast.
The only conclusions I can come up with are:
1) This is not an OS dependant issue.
2) spork, rspec and test unit don't play nicely on Rails > v3.x apps which makes sense. You don't need both. UPDATE - Please see updated answer below if you want both.
Obviously I followed the Railscast instructions very closely but I seriously suggest that you look at deleting the test folder.
UPDATE
It should be noted that it is possible to get both to play nicely together as per the comment below from #yuvilio, a quote of which follows:
I don't think the issue is that they don't get along. I got Cucumber/Rspec/testunit to play nice with each other in spork. In my gemfile, in addition to rspec/cucumber/guard related gems, I added spork-testunit, guard-test, ruby-prof gems and ran bundle install. Then, I bootstrapped testunit: bundle exec spork testunit --bootstrap.Then customized test/test_helper.rb. Then updated the guard file watching for testunit: bundle exec guard init test. When I ran guard, I got output including Spork server for RSpec, Cucumber, Test::Unit successfully started
If you don't want to delete the test folder, you can simply tell guard to ignore it:
guard 'spork', test_unit: false do
# ...
end

Testing one Cucumber feature takes minutes?

I've got the following gems installed for testing:
gem 'rspec-rails'
gem 'spork'
gem "factory_girl_rails"
gem "capybara"
gem "guard-rspec"
gem 'cucumber-rails'
gem 'database_cleaner'
All my tests take forever to run - testing one feature with Cucumber takes a good two minutes, and this is with Spork running with nearly everything in prefork.
The test itself took 0.544s on the last run, but it seems to be running cron.rake, rakefile, and I don't know what else. I checked these two files and they don't seem to have anything significant going on. Do you have any pointers for how I can improve this situation?
The readout:
San-iMac:app san$ rake cucumber features/user_sessions.feature
WARNING: Nokogiri was built against LibXML version 2.7.8, but has dynamically loaded 2.7.3
Running cron.rake
begin Rakefile
/Users/san/.rvm/rubies/ruby-1.9.2-p0/bin/ruby -S bundle exec cucumber --profile default
Using the default profile...
WARNING: Nokogiri was built against LibXML version 2.7.8, but has dynamically loaded 2.7.3
Feature: User Sessions
...
I suspect Rake is the problem. Try running cucumber directly instead, with "bundle exec cucumber" if you are using Bundler, or just "cucumber".
Are you using windows? We've noticed it takes much longer to spin up cucumber on windows than linux or mac.
I appreciate it might not be that easy to switch, but it'll typically save a couple of minutes per spin up.

How autotest in rails can run integration tests?

I have this in .autotest:
Autotest.add_hook :initialize do |autotest|
autotest.add_mapping(/^spec\/requests\/.*_spec\.rb$/) do
autotest.files_matching(/^spec\/requests\/.*_spec\.rb$/)
end
end
when I run autotest it initially runs all unit and integration (\requests) tests. Then when I change a file it runs only its unit tests and not the integrations.
any idea?
You need to install and require the fsevent gem.
So:
sudo gem install autotest-fsevent
Or just gem install depending if you are using RVM
Then in your .autotest put the following require:
require 'autotest/fsevent'
You should also insure that it isn't running the whole test suite by going into the window/tab that is running autotest and hit ctrl-c once. That will restart your autotest and run all the tests that autotest is covering. I have found that autotest runs the most recent spec first then all the tests.
Hope this helps!

autotest shows blank

I installed the autotest gem and intend to use it with rspec. The problem is, when I run autotest under my rails app, all I see is :
railsapp$ autospec
loading autotest/rails_rspec
And its stuck there until I Ctrl-C out of it. Nothing changes even if I change a rspec test or code.
Here's my ~/.autotest
require "autotest/restart"
require 'redgreen/autotest'
require 'autotest/fsevent'
require "autotest/growl"
I had the same problem. I was eventually able to get everything working by making sure I was on the latest (beta, if necessary) versions of rspec, rspec-rails, autotest, and autotest-rails, and putting the following in autotest/discovery.rb:
Autotest.add_discovery { "rails" }
Autotest.add_discovery { "rspec2" }
Here's the blog post that got me started in the right direction.
I had the same problem, and this fixed it:
"Make sure you have a .rspec file in the project root. That tells RSpec to tell Autotest to load RSpec’s autotest extension."
ref: https://github.com/rspec/rspec/wiki/autotest
It seems as if my .rspec file had gotten deleted when messing around with git.
Cd into app directory and run the command: AUTOFEATURE=true autospec
To stop this process ^C twice
I had the same problem with Rails 2.3.2 except that I'm not using RSpec. In my case, installing the autotest-rails-pure gem got it working for me. Maybe autotest requires the correct plugin in order to detect a test within a file.

Resources