-
Notifications
You must be signed in to change notification settings - Fork 553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spork doesn't seem to run SimpleCov correctly with RSpec and Rails #42
Comments
FWIW, I'm running |
I'm not using Spork myself, but from what I understand is that it always spins up a new process when the test suite should be run. The Ruby Coverage library SimpleCov uses works by tracking every So I imagine the solution would be to make Spork |
Quick update: I pinged @timcharper about this, hopefully he can enlighten us a bit :) |
I too am having this issue. Seems not many people run into this problem, as I can't find anything anywhere else. |
@fj, I bet your problem is your test environment caches classes. See @Estheth's comment in #30. fwiw, I had been using the hack below in
|
I have this issue, i put the two lines for simplecov at the top of the |
+1. Same applies to configuration with cucumber. I bet it's spork related |
+1. Same config as @Godisemo along with the same issue. |
+1 Same here. I swear, it used to work.... |
+1 simplecov (0.5.4) |
Guys, this obviously is related to #60 - the offender being Spork's requirement to set |
@colszowka I had |
Something must be wrong in your setups. I successfully use both |
You guys should try using the Coverage library directly in your setups. If it works, then SimpleCov needs fixing. Else, you know what to do. :) |
I'm afraid I don't have any experience with SimpleCov to be able to shed much light on this. But, I can say this: You shouldn't need https://github.com/sporkrb/spork/wiki/Troubleshooting (specifically: Some changes to files don’t take effect until I restart Spork.) https://github.com/sporkrb/spork/wiki/Spork.trap_method-Jujutsu All you should need to do is delay the initialization of SimpleCov until after the process forks, and SimpleCov should be none the wiser. If a SimpleCov devel wants to do some remove pair programming with me to crack this together, I'd be happy to schedule some time. |
On Thu 09 Feb 2012 10:44:56 AM PST, Tim Harper wrote:
That is correct. You should call https://github.com/sunaku/tork/blob/coverage/lib/tork/config/coverage.rb#L6-10 |
FWIW, this is working for me now. I initially had some issues and after a bunch of mucking about it now works (even though my config is right where it was when I started). The one minor change I made to my code was to skip my custom initializer which bootstrapped some of my custom libs. I now require the initializer manually in the each_run block. This is the complete contents of my test_helper.rb (I'm using minitest): https://gist.github.com/1820849 I also disabled cache_classes as noted above, though I'm not sure that's absolutely necessary. |
So I'm not familiar with Tork, but @sunaku is the ordering of that @chetan It's not clear what you're doing in the files you require from your Gist -- if you're loading your app env in |
@ches oops, I forgot to attach test_prefork. Basically I bootstrap the whole rails and test environment w/ the exception of all app/ lib/ and test/ classes. My test runtime has gone from about 10sec (mostly in Bundle.setup) to 1sec. Updated the gist: |
I'm having the same issue, following the same logic grid as in the first report. I tried toggling class caching in the test environment to no avail. The coverage report just doesn't run unless I manually run Versions:
spec_helper.rb https://gist.github.com/1934043 |
For me, SimpleCov works when Spork is shut down, but not when Spork is running. I tried all the suggestions above (and from other Google searches on the same problem) to no avail; in particular, setting caching to true or false makes no difference. In the end, I learned two things:
Don't know if this will help anyone, but at least I have a way to get a coverage report without shutting Spork down every time. |
This works: Spork.prefork do
unless ENV['DRB']
require 'simplecov'
SimpleCov.start 'rails'
end
# other code ...
end
Spork.each_run do
if ENV['DRB']
require 'simplecov'
SimpleCov.start 'rails'
end
# other code ...
end |
@fredwu nice, it's working for me now. |
@fredwu Works for me too! ruby - 1.9.3 |
@fredwu Excellent! That was the piece I needed to unlock the puzzle. There were two other things I had to do:
Now I get identical results for "rake spec" and "rspec spec", with and without Spork running. Thank you! (I still have "config.cache_classes = false" by the way.) |
I am getting irregular coverage results with rspec tests while running against spork, despite applying @fredwu 's fix and playing with stuff like:
For now I am simply going to leave it alone since the trouble proved to be too much for any potential gain (for those interested, my setup: https://github.com/prusswan/hw3_rottenpotatoes/tree/hw4) |
@prusswan Your repo's Gemfile doesn't have |
@fredwu as I said I have already tried changing those parameters but they did not improve matters, the result with rspec is pretty erratic. |
@prusswan and others, the solutions suggested here got me part of the way. I think the problem is when some of the code you want coverage reports for is run for the first time during Rails initialization (for example, if an initializer in config/initializers does some config on models or custom code in your lib directory or whatever). SimpleCov won't pick that stuff up because it's loaded before SimpleCov is. A solution is to split loading and initialization between prefork and each_run by requiring application.rb instead of environment.rb, and then explicitly initializing your app in each_run. This solution works with config.cache_classes = true and if threadsafe mode has not been turned on. A detailed blog post is here: http://rickyrobinson.id.au/2012/07/20/when-spork-puts-a-fork-in-your-cucumber-and-a-spanner-in-your-specs. |
I still can't tell much about spork, but on the Ruby Toolbox I've been using spin(+guard-spin) with great success recently. It basically only preloads your app's gems (so probably there's a little time delay compared to spork), but works without any config, and it works smoothly with simplecov so far (of course, only mentioning coverage for the latetst spec that's been run - you can avoid this by expanding |
@colszowka Hey, spin looks pretty cool. Thanks for the pointer. It runs my specs just fine after I take out all the spork-related stuff, but not sure if it'll run my cukes. I note that spin takes the same approach to splitting loading and initialization that my spork hack takes:
http://jstorimer.github.com/spin/#section-9 Update: got spin working with cucumber and made a few other changes: http://rickyrobinson.id.au/2012/07/24/spork-to-spin |
Having the simplecov runs fine with spork. But the report generated is missing the helper files. But once I stopped spork and run all the specs, then the report includes helper files. Any idea? Anyone have the same issue or know how to fix it? |
@fredwu's workaround works for me only if I don't add an
When I do it I need to configure I don't feel comfortable disabling |
An additional observation: If you're using ActiveRecord observers, and activating them from your The observed models being skipped seems to only be an issue when I run my specs through Spork. |
Works for me: I followed the "this works" suggestion by fredwu. It didn't work with Spork at first. Then I additionally followed bjnord's suggestion to add I should note though, that in my test.rb, the following is still unchanged, and that seems to not cause a difference. It still works without modification: Further update: In In your spec, do: |
I had the same issue with some application_helper lines that were not evaluated as covered code when running guard+spork. When running rspec without guard, coverage was 100% for that helper. I use a gem that is located under vendor/gems at the moment which is not watched by guard, I excluded it with a filter in the .simplecov file. I removed the vendor-filter and added :spec_paths => ["spec", "vendor/gems/.../spec"] to the Guardfile, so the gem-tests are now run together with my rails tests. With this configuration, the coverage is correct with AND without guard/spork. |
SimpleCov will only pickup if we pass the Though I'm not sure why... |
Here is how I solved the problem in environment/test.rb in spec_hepler.rb Spork.each_run do
end |
I am using Sinatra, RSpec, Guard, Spork, and SimpleCov, and I had the same issue where code metrics weren't being generated properly while running with Guard. Posting here, with a complete This was my initial spec_helper, and the tip above where you checked for the DRB env var didn't work for me. This is what my # Note: Ideas from http://blog.codenursery.com/2011/11/adding-rspec-to-sinatra.html
# and http://www.iamzp.com/blog/2013/04/12/test-driven-development-with-sinatra-rspec-and-guard/
require 'rubygems'
require 'spork'
ENV['RACK_ENV'] = 'test'
def require_files
ruby_files = File.join(File.dirname(__FILE__), '..', 'lib', '**', '*.rb')
Dir[ruby_files].each do |file|
require file
end
require File.join(File.dirname(__FILE__), '..', 'app.rb')
end
Spork.prefork do
require_files
require 'rubygems'
require 'sinatra'
require 'rspec'
require 'rack/test'
# Set up the test env
set :environment, :test
set :run, false
set :raise_errors, true
set :logging, false
def app
@app ||= Sinatra::Application
end
RSpec.configure do |config|
config.include Rack::Test::Methods
end
end
Spork.each_run do
end I used some of the ideas above to change it to what follows, which generates the correct code coverage metrics: # Note: Ideas from http://blog.codenursery.com/2011/11/adding-rspec-to-sinatra.html
# and http://www.iamzp.com/blog/2013/04/12/test-driven-development-with-sinatra-rspec-and-guard/
require 'rubygems'
require 'spork'
ENV['RACK_ENV'] = 'test'
def require_files
ruby_files = File.join(File.dirname(__FILE__), '..', 'lib', '**', '*.rb')
Dir[ruby_files].each do |file|
require file
end
require File.join(File.dirname(__FILE__), '..', 'app.rb')
end
Spork.prefork do
require 'rubygems'
require 'sinatra'
require 'rspec'
require 'rack/test'
# Set up the test env
set :environment, :test
set :run, false
set :raise_errors, true
set :logging, false
def app
@app ||= Sinatra::Application
end
end
Spork.each_run do
require 'simplecov'
SimpleCov.start do
add_filter '/spec/'
end
require_files
RSpec.configure do |config|
config.include Rack::Test::Methods
end
end |
ref: #340 |
Closing as stale |
tl;dr:
Summary of results:
I'm on an existing Rails project using Mongoid, Devise, RSpec, and Spork (0.9.0.rc5). Our
spec/spec_helper.rb
looks like this:This configuration will run SimpleCov when Spork is not running, and it produces the expected result. When Spork is running, no coverage message is shown.
If I move the
require 'simplecov'; SimpleCov.start 'rails'
lines to the beginning of the Spork.each_run block, as SimpleCov's readme suggests, then when Spork is not running, I get this:If Spork is running, then instead I get no coverage message, as before.
The text was updated successfully, but these errors were encountered: