Skip to content

Instantly share code, notes, and snippets.

@dogweather
Forked from j1n6/01 example_spec.rb
Last active January 4, 2016 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dogweather/8642881 to your computer and use it in GitHub Desktop.
Save dogweather/8642881 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"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment