How can I configure rspec to show output with spork?
Asked Answered
A

2

5

I have spork running to speed up my tests but there is no output when I run them. Is there a configuration that I need to modify?

Aboulia answered 7/3, 2014 at 0:57 Comment(0)
B
7

Just ran into this as well, running on spork 1.0.0rc4 and rspec 2.14.1 / rspec-core 2.14.8 . As far as I could figure it out, the problem lies in the following:

  1. Spork configures the $stdout to point to localhost:port (drb client) so that all reporter output is sent to the drb client. This now happens after the Spork.prefork is ran.
  2. In my Spork.prefork block I was (naturally) doing RSpec.configure do |config|, and one of the config calls there (in particular config.mock_with :rr), was causing a sequence of calls that eventually caused the reporter/formatters in RSpec.configuration to be initialized - but without the incorrect stdout.
  3. RSpec.configuration has a reset method (marked as @private but alas), which flushes the initialized reporter/formatters, to be lazily reinitialized.
  4. Also, somehow RSpec.configuration.output_stream is never set. Bottom line, adding this code to Spork.each_run appears to fix the problem:

    if Spork.using_spork?
        RSpec.configure do |config|
            config.reset
            config.output_stream = $stdout
        end
    end
    

If anyone knows of a more elegant way to solve this, please tell!

Betatron answered 16/3, 2014 at 21:37 Comment(3)
Thanks! This does work when I run rspec from the command line. However, I still don't see any output when I run my specs from vim. Seems like I just need to figure out the right way to set the output_stream for vim.Aboulia
Put a breakpoint in your Spork.each_run and see what $stdout is set to, you might indeed have to play with it. Also, I realized that the block above should be inside a if Spork.using_spork? so that it doesn't affect CI performance etc. (which runs without spork).Betatron
Had the same issue but in my case config.output_stream = $stdout solved it. ThanksMaxine
T
0

@astgtciv already answered the question, but here's a related thing I ran into that is too long for writing as a comment:

Using @astgtciv's fix worked for me, but it broke again when I introduced a custom formatter. My guess is that the formatter is initialized when Spork boots, so its output stream is in the DRB server window instead of the test runner window.

My fix was to add attr_accessor :output to my custom formatter, then change the output inside Spork.each_run, e.g:

rspec_formatters = RSpec.configuration.formatters

Spork.each_run do
  if Spork.using_spork?
    RSpec.configure do |config|
      config.output_stream = $stdout
    end

    rspec_formatters.each do |formatter|
      if formatter.respond_to?(:output=)
        formatter.output = $stdout
      end
    end
  end
end

A simpler fix would be to just add the formatter inside this code block, but I thought this might be faster (in terms of test run speed) and not risk adding the same formatter multiple times. Have not verified that guess though.

Tetra answered 10/6, 2016 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.