Running RSpec within Ruby? Changes in v. 3?

70 views
Skip to first unread message

Robb Shecter

unread,
Jan 24, 2016, 4:11:35 AM1/24/16
to rspec
Hi,

This used to work:

config = RSpec.configuration

json_formatter = RSpec::Core::Formatters::JsonFormatter.new(config.out)
reporter =  RSpec::Core::Reporter.new(json_formatter)
config.instance_variable_set(:@reporter, reporter)

RSpec::Core::Runner.run(["#{Rails.root}/spec/test1_spec.rb"])

puts json_formatter.output_hash 

Now, however, I receive this error:

undefined method `out' for #<RSpec::Core::Configuration

Has the Configuration interface changed?

Robb Shecter

unread,
Jan 24, 2016, 4:32:00 AM1/24/16
to rspec
Yes, the interfaces here in RSpec 3 have changed. I found a good answer on Stack Overflow. It does use this mysterious code, though:

# internal hack
# api may not be stable, make sure lock down Rspec version
loader = config.send(:formatter_loader)
notifications = loader.send(:notifications_for, RSpec::Core::Formatters::JsonFormatter)
reporter.register_listener(formatter, *notifications)

I'm not sure what that's about.

Allen Madsen

unread,
Jan 24, 2016, 10:49:40 AM1/24/16
to rs...@googlegroups.com
I don't understand why you're modifying stuff through non-public
interfaces. Is there a reason you can't use
`config.default_formatter=`?

http://www.rubydoc.info/gems/rspec-core/RSpec/Core/Configuration#default_formatter%3D-instance_method
Allen Madsen
http://www.allenmadsen.com
> --
> You received this message because you are subscribed to the Google Groups
> "rspec" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rspec+un...@googlegroups.com.
> To post to this group, send email to rs...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rspec/52a47979-6502-41c9-bccf-1602e36aa4d3%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Jon Rowe

unread,
Jan 24, 2016, 6:36:22 PM1/24/16
to rs...@googlegroups.com
Hi Robb

What are you trying to achieve with this? I might be able to find you a way to do this without using private APIs / interfering with the RSpec internals.

Cheers
Jon Rowe
---------------------------

Robb Shecter

unread,
Feb 13, 2016, 4:44:18 AM2/13/16
to rspec
On Sunday, January 24, 2016 at 3:36:22 PM UTC-8, Jon Rowe wrote:
Hi Robb

What are you trying to achieve with this? I might be able to find you a way to do this without using private APIs / interfering with the RSpec internals.


Thanks for the feedback. I'm now using the public API, although this is probably incorrect, and less than efficient. 

I'm generating RSpec on the fly in string form, executing it, and then using the results. This is a web app compliance monitoring service, Nonstop QA.

# The strategy I'm using in http://nonstop.qa. This has
# a messy feel, and I'm rethinking it. It's also possible
# I'm not sure I'm using the RSpec API correctly or as 
# efficiently as possible.

# I first prepare RSpec's output. 
def prepare_rspec
  output = StringIO.new('', 'w')
  RSpec.configure do |rspec|
    rspec.default_formatter = 'json'
    rspec.output_stream = output
  end
  output
end

# Then, I invoke this with each spec I need to run,
# which is in string form.
def run_spec(source_code, output)
  file = create_temp_file(source_code)
  RSpec::Core::Runner.run([file.path])
  json_string = output.string
  output.reopen('', 'w')
  file.unlink
  JSON.parse(json_string)
end

Robb Shecter

unread,
Feb 13, 2016, 4:45:47 AM2/13/16
to rspec
It looks pretty clear that I'm doing too much work, and not using the inputs and outputs efficiently. I'm thinking of switching to simply shelling out, and reading the json formatter's stdout.

Jon Rowe

unread,
Feb 13, 2016, 6:20:01 PM2/13/16
to rs...@googlegroups.com
If you’re creating and/or consuming the spec files outside of ruby I would abolsultely consider shelling out here, however if you’re creating the specs inside Ruby have you considered creating them programatically rather than writing them to files?

Jon Rowe
---------------------------

On Saturday, 13 February 2016 at 19:45, Robb Shecter wrote:

It looks pretty clear that I'm doing too much work, and not using the inputs and outputs efficiently. I'm thinking of switching to simply shelling out, and reading the json formatter's stdout.

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.

Robb Shecter

unread,
Feb 14, 2016, 9:14:52 PM2/14/16
to rspec
On Saturday, February 13, 2016 at 3:20:01 PM UTC-8, Jon Rowe wrote:
...if you’re creating the specs inside Ruby have you considered creating them programatically rather than writing them to files?

Absolutely! That's what I'd like to do, but I haven't been able to figure out how from the API, http://www.rubydoc.info/github/rspec/rspec-core/RSpec/Core/Runner. I haven't seen documentation for doing this, or do you know of some?

Jon Rowe

unread,
Feb 14, 2016, 9:42:28 PM2/14/16
to rs...@googlegroups.com
`RSpec.describe` is just Ruby and takes a block describing specs, what I think will work for you is something like (obviously slightly pseudo code):

```
output = StringIO.new('', 'w')

RSpec.configure do |rspec|
  rspec.default_formatter = 'json'
end

[things, to, spec].each do |item|
  RSpec.describe item.description do
    item.checks.each do |spec|
      it spec.title, &spec.test_block
    end
  end
end

exist_status = RSpec::Core::Runner.run([], $stderr, output)
```

Cheers
Jon Rowe
---------------------------

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages