Created
February 23, 2010 16:39
-
-
Save mag/312383 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The problem with the above is that the two rspec "worlds" get mixed together. Despite passing a separate Options object to | |
my rpsec builder, it ends up using the same Reporter object as the spec itself! So at the end of the run rspec reports that | |
it ran 2 specs - the one run by RspecBuilder and the one run by the spec itself. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RspecBuilder | |
require 'spec' | |
# 'suites' is data structure that can be converted to a nested rspec example group | |
def initialize(suites, spec_options) | |
@suites = suites | |
@spec_options = spec_options | |
end | |
def run | |
declare_suites | |
@spec_options.run_examples | |
end | |
def declare_suites | |
@suites.each do |suite| | |
declare_suite(self, suite) | |
end | |
end | |
def declare_suite(parent, suite) | |
me = self | |
parent.describe suite["name"] do | |
suite["children"].each do |suite_or_spec| | |
type = suite_or_spec["type"] | |
if type == "suite" | |
me.declare_suite(self, suite_or_spec) | |
elsif type == "spec" | |
me.declare_spec(self, suite_or_spec) | |
else | |
raise "unknown type #{type} for #{suite_or_spec.inspect}" | |
end | |
end | |
end | |
end | |
def declare_spec(parent, spec) | |
example_name = spec["name"] | |
parent.it example_name do | |
# run spec here | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'spec' | |
require 'spec/autorun' | |
describe RspecBuilder do | |
it "runs the given specs and reports results like an rspec" do | |
error_stream = StringIO.new | |
out_stream = StringIO.new | |
options = Spec::Runner::Options.new(error_stream, out_stream) | |
suites = [] # this would build the representation | |
builder = RspecBuilder.new(suites) | |
builder.spec_options = options | |
builder.run | |
# assertions here | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment