سؤال

I've got a test that I want to run to test that I'm recieving output to STDOUT in Ruby.

Right now my test looks like this:

STDOUT.sync = true
@orig_stdout_constant = STDOUT
STDOUT  = StringIO.new

subject.request(:get, '/success')

expect(STDOUT.string).to include 'INFO -- : get https://api.example.com/success', 'INFO -- : get https://api.example.com/success'

STDOUT  = @orig_stdout_constant

Which works and the test passes, but it feels very hacky and you'll get Ruby warning errors about an already initialized constant.

I know you can do this:

subject.request(:get, '/success')

STDOUT.should_receive(:print).with("I, [2014-02-11T14:55:00.282124 #650]  INFO -- : get https://api.example.com/success")

But the string as you can see has two values that will change every time the test is run: the time (which I can fix with TimeCop but prefer not to) and the id of the run (which is set in Faraday, I could stub, but again: I'd prefer not to...)

So the crux of what am asking is: is there a way to do STDOUT.should_receive(:print).with(/[\s\S]+ INFO -- : get https:\/\/api.example.com\/success/) or do a some sort of matching on the recieve?

The full code is here if it helps: https://github.com/petems/oauth2/blob/faraday_debug/spec/oauth2/client_spec.rb#L123

هل كانت مفيدة؟

المحلول

Yes, you can do exactly what you proposed, as with accepts a regex as an argument, as in:

describe "RSpec's #with method" do
  it "accepts a regex" do
    object = Object.new
    object.should_receive(:foo).with(/bar/)
    object.foo('foobar')
  end
end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top