add helper for specs testing std{out,err}
authorLunar <lunar@anargeek.net>
Sat, 10 Mar 2012 10:06:37 +0000 (11:06 +0100)
committerLunar <lunar@anargeek.net>
Thu, 14 Mar 2013 09:12:08 +0000 (10:12 +0100)
spec/spec_helper.rb

index 2546dde..087905b 100644 (file)
@@ -22,6 +22,7 @@ Bundler.setup
 
 require 'rack/test'
 require 'rspec'
+require 'stringio'
 
 require 'coquelicot'
 
@@ -71,4 +72,23 @@ module StoredFileHelpers
   end
 end
 
-::RSpec.configure { |c| c.extend StoredFileHelpers }
+module CoquelicotSpecHelpers
+  # written by cash on
+  # http://rails-bestpractices.com/questions/1-test-stdin-stdout-in-rspec
+  def capture(*streams)
+    streams.map! { |stream| stream.to_s }
+    begin
+      result = StringIO.new
+      streams.each { |stream| eval "$#{stream} = result" }
+      yield
+    ensure
+      streams.each { |stream| eval("$#{stream} = #{stream.upcase}") }
+    end
+    result.string
+  end
+end
+
+::RSpec.configure do |c|
+  c.extend StoredFileHelpers
+  c.include CoquelicotSpecHelpers
+end