#285 Spork
Spork improves the loading time of your test suite by starting up your Rails application once in the background. Use it with Guard for the ultimate combo in fast feedback while doing TDD.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
Yeah.. Thank you Ryan.. This is the informative about testing.
I want to share this link for also spork for fast testing:
http://blog.carbonfive.com/2010/12/10/speedy-test-iterations-for-rails-3-with-spork-and-guard/
I used the spork and guard for many month now, but just realized there is :focus and "return" to run all spec.
usualy i have to modify cucumber.yml to add tags. and run all by pressing ctrl +
thanks ryan
Has anyone had any trouble getting spork to actually start running specs quicker?
My existing app's specs actually take longer to get running through spork and I can't figure out why.
I have even tried with a fresh rails 3.1 app with no actual specs, and running rspec via spork takes around 20 seconds vs 0.3 seconds without spork.
It's like there's some delay in rspec actually connecting to spork.
I have the same problem with spork. I'm working on a relatively new Rails 3.1 project and have just over 500 examples and running my specs with spork takes longer than without.
There's also a little gotcha to even more decrease feedback time: just try add :bundler => false to both rspec-guard and spork-guard configurations in Guardfile and if you can run these commands directly (not through bundler), you'll see that total run time will be almost equal to testing time.
That's so because by default guard runs all commands through "bundle exec" which takes some time to initialize.
Awesome! It pays to read the documentation. I'm looking through them now to see what else I missed...
Great screencast. But - on spork, some of my specs fail, that do not fail on local (non drb) testing. Anyone has ideas?? :(
It seems it has something to do with rspec mocking, example of one of the errors (repeating itself over the entire spec file)
Failure/Error: @order.station = mock_model("Station")
NoMethodError:
undefined method `primary_key' for Station:Class
code is
Ok, it seems like something that occurred in rails 3.1, that for some reason did not show its ugly face on normal rspec exec, but does on --drb.
mock_model should not be used with class name, rather it should be used with actual class now.
I could find the documentation for :focus in guard-spec ? where did you find it ?
For testing I use rspec, spork, guard, guard-spork, capybara, capybara-webkit, database-cleaner, factory-girl, vcr, aruba, rcov, jasmine, and jasmine-rails. And there are lots of other tools that I could be using (cucumber, mocha, etc. etc.)
To me it feels like the world of Rails testing is out of control. This kind of complexity is why I left Java!!
True, the "default" Rails stack has grown a lot in the last couple of years. But previously we just didn't do this kind of advanced testing when hacking Rails. We loved the simplicity back then, but did also miss out on a lot of advanced features. Now that we have them we miss the innocent days ;)
I'm not having any luck it seems at using spark in conjunction with factory_girl. Factory girl causes my models not to be evaluated again after a change. Have you experienced anything like this using spark and factory_girl?
I had the same problem. Could not get models reload. I tried to a bunch of fixes I found from searches, but nothing worked. I eventually had to abandon spork. Sucks too cause it did speed it up a bunch.
I think spork caches classes. That means you need to reload spork whenever any of your non-view app/ files change. For example, I have this (and similar lines) in my guard 'spork' block:
watch(%r{^app/models/.+\.rb$})
I just use Spork each_run method to reload model files that Ryan mentioned in this screencast.
Spork.each_run do
# This code will be run each time you run your specs.
# Reload all model files when run each spec
# otherwise there might be out-of-date testing
require 'rspec/rails'
Dir["#{Rails.root}/app/controllers//*.rb"].each do |controller|
load controller
end
Dir["#{Rails.root}/app/models//*.rb"].each do |model|
load model
end
end
Thank you!
I prefere Earle Clubbs method. Besides turn off cache classes when you are using spork with guard.
http://www.avenue80.com/tiny-tip-spork-not-reloading-classes/
pengpenglin's method didnt work quite well with model validations and cache counters.
+1
The problem is caused by preloading ActiveRecord during initialization, not FactoryGirl. You probably are using a gem that does not use ActiveSupport.on_load correctly, or one of your initializers references ActiveRecord::Base. That's bad.
can you developp ?
i could make spork relaod classes , but not when used with guard
Does somebody have a solution not to load theese damn models classes (reloading spork is so slow ...)
Added Cucumber and ran
spork cucumber --bootstrap
to ensure it bootstrappedenv.rb
, features are now running twice. In fact, launching guard loads the rails environment twice as well?That's perfectly normal. You have one spork instance for rspec and one for cucumber.
I had to set
:cli => '--drb'
as well as bootstrap cuke's env.rb. Working fine now =)My features are running twice too. Is that normal? I understand that two spork environments are requires, but surely my features should only run once?
Ryan,
Great screencast, and as typical a very timely topic.
Anyway, another way to get rspec to use spork instead of the
option is to put "--drb" in the .rspec file at the project root dir. I also include "--colour" and "--format progress" (or "--format nested") there.
I found that if I put --drb in both .rspec and GuardFile, there will be a warning that says "port is in use", so I move the --drb into GuardFile and left --color in .rspec, that warning is gone.
BTW, set :cli => "--color --format nested --fail-fast --drb" will makes the output of Rspec more readable and clear
On Windows, I get the error
> 'spork' is not recognized as an internal or external command,
> operable program or batch file.
When I ran bundler, though, spork installed 3 windows gems
Try running it like this:
Great cast! Can spork be modified to be used for rails console as well? It seems like preloading the environment would be useful in a variety of contexts...
When switching to spork I encountered a string of errors like this:
This happened when using the
gem 'growl_notify'
. Switching my Gemfile over to just usegem 'growl'
fixed this error.if you are interested in having really fast tests, have a look at corey haines talk: lanyrd.com/2011/gogaruco/shmhw/
i cannot see my model changes using cache_classe=false in test config, i had to add watch(%r{^app/models/.+.rb$}) into my guard file to reload spork but this is realy slow does anybody have an other solution ?
i use meta_where do you think it preloads classes ?
I followed by letter the screencast tuto but got the below errors:
Any idea about the error? Thanks
What could also speed up your process is to tell your specs to halt after first failure:
Everything seems to be working just fine for me except one thing. Every time I save a spec file and those tests get run, the time that RSpec says they finished in is actually the amount of time that Guard has been running. So the time in seconds just keeps getting bigger. Has anyone else encountered this? Any ideas how to fix it?
I'm using Ruby 1.8.7, Rails 3.1, and everything else is pretty much the same as the screencast.
Just noticed the same behaviour here, kinda weird.
Does this help? Fixed it for me.
https://github.com/sporkrb/spork/issues/151
The hack does fix it, thanks.
For anyone struggling to get this going with Test::Unit, you can check out my simple setup here: http://railsgotchas.wordpress.com/2011/12/16/testing-with-spork-guard-and-testunit/
Guys, I have everything working fine (spark, guard, rspec, and growl) but when spark runs everything I don't get any spec output through spork?
I removed the :cli => --drb from the Guard file, as there is the --drb in the .rspec file. It is working now, but growl is not.
Anyone?
Installing growl-notify from http://growl.info/downloads fixed the problem for me. Hope this helps.
You may need to set the following in your environments/tests.rb file to avoid problems down the track:
For some reason caching seemed to randomly kick in for one of my models while I was testing it and after that none of my changes to the model had any affect. I'm not the only one to have experienced this:
http://www.avenue80.com/tiny-tip-spork-not-reloading-classes/#comment-133
Perhaps I did something wrong, but I don't think so and changing that line fixed the problem.
I ran in to the following problem: "ERROR: Could not start Spork server for Test::Unit & RSpec".
To fix this issue I added the following to my Guardfile.
guard 'spork', :test_unit => false, :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do
Same problem, different solution
http://stackoverflow.com/questions/8177331/problems-with-guard-spork-rspec-rails-3
remove the test folder, which i guess result in test_unit not being used
Spork gem doesn't support Rails directly anymore. Now, you should use spork-rails:
https://github.com/sporkrb/spork-rails
You can also use zeus as alternative to spork:
https://github.com/burke/zeus
Comparing to spork it has zero configuration and speed up not only tests executing but all rails commands as well:
zeus console, zeus server, etc.