RSpec Code Examples

 

 

pending spec with no implementation
is pending (PENDING: Not yet implemented)
pending command with block format
with content that would fail
is pending (PENDING: No reason given)
behaves like shared
is marked as pending but passes n.nnnns
  Expected pending 'No reason given' to fail. No error was raised.
  Shared Example Group: "shared" called from ./spec/rspec/core/resources/formatter_specs.rb:22
  # ./spec/rspec/core/resources/formatter_specs.rb:4
./spec/rspec/core/resources/formatter_specs.rb:4
2
3RSpec.shared_examples_for "shared" do
4  it "is marked as pending but passes" do
5    pending
6    expect(1).to eq(1)
passing spec
passesn.nnnns
passes with a multiple line descriptionn.nnnns
failing spec
fails n.nnnns
  Failure/Error: expect(1).to eq(2)

    expected: 2
         got: 1

    (compared using ==)
  # ./spec/rspec/core/resources/formatter_specs.rb:37:in `block (2 levels) in <top (required)>'
./spec/rspec/core/resources/formatter_specs.rb:37:in `block (2 levels) in <top (required)>'
35RSpec.describe "failing spec" do
36  it "fails" do
37    expect(1).to eq(2)
38  end
fails twice n.nnnns
  Got 2 failures:

  1) Failure/Error: expect(1).to eq(2)

       expected: 2
            got: 1

       (compared using ==)
     # ./spec/rspec/core/resources/formatter_specs.rb:41:in `block (2 levels) in <top (required)>'

  2) Failure/Error: expect(3).to eq(4)

       expected: 4
            got: 3

       (compared using ==)
     # ./spec/rspec/core/resources/formatter_specs.rb:42:in `block (2 levels) in <top (required)>'
-1# Couldn't get snippet for 
a failing spec with odd backtraces
fails with a backtrace that has no file n.nnnns
  Failure/Error: ERB.new("<%= raise 'foo' %>").result

  RuntimeError:
    foo
  # ./spec/rspec/core/resources/formatter_specs.rb:50:in `block (2 levels) in <top (required)>'
./spec/rspec/core/resources/formatter_specs.rb:50:in `block (2 levels) in <top (required)>'
48    require 'erb'
49
50    ERB.new("<%= raise 'foo' %>").result
51  end
fails with a backtrace containing an erb file n.nnnns
  Failure/Error: Unable to find /foo.html.erb to read failed line

  Exception:
    Exception
  # /foo.html.erb:1:in `<main>': foo (RuntimeError)
  #    from /lib/ruby/1.9.1/erb.rb:753:in `eval'
  # 
  #   Showing full backtrace because every line was filtered out.
  #   See docs for RSpec::Configuration#backtrace_exclusion_patterns and
  #   RSpec::Configuration#backtrace_inclusion_patterns for more information.
/foo.html.erb:1:in `<main>': foo (RuntimeError)
   from /lib/ruby/1.9.1/erb.rb:753:in `eval'

  Showing full backtrace because every line was filtered out.
  See docs for RSpec::Configuration#backtrace_exclusion_patterns and
  RSpec::Configuration#backtrace_inclusion_patterns for more information.
-1# Couldn't get snippet for 
with a `nil` backtrace
raises n.nnnns
  Failure/Error: Unable to find matching line from backtrace

  RuntimeError:
    boom
-1# Couldn't get snippet for