Skip to content

Instantly share code, notes, and snippets.

@j1n6
Last active August 3, 2019 11:33
Show Gist options
  • Save j1n6/4467752 to your computer and use it in GitHub Desktop.
Save j1n6/4467752 to your computer and use it in GitHub Desktop.
require 'rspec'
describe "Behaviour" do
it "should pass" do
true.should eq true
end
it "should fail" do
true.should eq false
end
it "should pending"
end
require 'rspec'
require 'rspec/core/formatters/json_formatter'
config = RSpec.configuration
json_formatter = RSpec::Core::Formatters::JsonFormatter.new(config.out)
# create reporter with json formatter
reporter = RSpec::Core::Reporter.new(json_formatter)
# set reporter for rspec configuration
config.instance_variable_set(:@reporter, reporter)
# execute rspec runner
# 'example_spec.rb' is the location of the spec file
RSpec::Core::Runner.run(['example_spec.rb'])
# output test result as json
# see example output in `rspec_json_formatter_result.rb`
puts json_formatter.output_hash
{
:examples => [{:description => "should pass",
:full_description => "Behaviour should pass",
:status => "passed",
:file_path => "./my_spec.rb",
:line_number => 4
}, {:description => "should fail",
:full_description => "Behaviour should fail",
:status => "failed",
:file_path => "./my_spec.rb",
:line_number => 8,
:exception => {:class => "RSpec::Expectations::ExpectationNotMetError",
:message => "\nexpected:false\n got:true\n\n(compared using ==)\n",
:backtrace => ["/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/expectations/fail_with.rb:33:in `fail_with'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/expectations/handler.rb:31:in `handle_matcher'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/expectations/syntax.rb:53:in `should'", "/Users/jing/Documents/other/test/my_spec.rb:9:in `block (2 levels) in <top (required)>'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:114:in `instance_eval'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:114:in `block in run'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:254:in `with_around_each_hooks'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:111:in `run'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:388:in `block in run_examples'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:384:in `map'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:384:in `run_examples'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:369:in `run'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `map'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `block in run'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/reporter.rb:34:in `report'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:25:in `run'", "/Users/jing/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:80:in `run'", "runner.rb:19:in `<main>'"]
}
}, {:description => "should pending",
:full_description => "Behaviour should pending",
:status => "pending",
:file_path => "./my_spec.rb",
:line_number => 12
}],
:summary => {:duration => 0.001543,
:example_count => 3,
:failure_count => 1,
:pending_count => 1
},
:summary_line => "3 examples, 1 failure, 1 pending"
}
@BenGood
Copy link

BenGood commented Feb 16, 2016

if anyone can comment back on how to pass variables to rspec tests I would appreciate it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment