# RSpec Code Examples

Mocker
  should be able to call mock()
  should fail when expected message not received

      Mock "poke me" expected :poke with (any args) once, but received it 0 times

      examples/failing/mocking_example.rb:11:
      spec/spec_helper.rb:42:in `run_with'
      ./spec/spec/runner/formatter/html_formatter_spec.rb:41:
      ./spec/spec/runner/formatter/html_formatter_spec.rb:29:in `chdir'
      ./spec/spec/runner/formatter/html_formatter_spec.rb:29:

      9  it "should fail when expected message not received" do
      10    mock = mock("poke me")
      11    mock.should_receive(:poke)
      12  end
      13

  should fail when messages are received out of order

      Mock "one two three" received :three out of order

      examples/failing/mocking_example.rb:20:
      spec/spec_helper.rb:42:in `run_with'
      ./spec/spec/runner/formatter/html_formatter_spec.rb:41:
      ./spec/spec/runner/formatter/html_formatter_spec.rb:29:in `chdir'
      ./spec/spec/runner/formatter/html_formatter_spec.rb:29:

      18    mock.should_receive(:three).ordered
      19    mock.one
      20    mock.three
      21    mock.two
      22  end

  should get yelled at when sending unexpected messages

      Mock "don't talk to me" expected :any_message_at_all with (no args) 0 times, but received it once

      examples/failing/mocking_example.rb:27:
      spec/spec_helper.rb:42:in `run_with'
      ./spec/spec/runner/formatter/html_formatter_spec.rb:41:
      ./spec/spec/runner/formatter/html_formatter_spec.rb:29:in `chdir'
      ./spec/spec/runner/formatter/html_formatter_spec.rb:29:

      25    mock = mock("don't talk to me")
      26    mock.should_not_receive(:any_message_at_all)
      27    mock.any_message_at_all
      28  end

  has a bug we need to fix

      Expected pending 'here is the bug' to fail. No Error was raised.

      examples/failing/mocking_example.rb:31:
      spec/spec_helper.rb:42:in `run_with'
      ./spec/spec/runner/formatter/html_formatter_spec.rb:41:
      ./spec/spec/runner/formatter/html_formatter_spec.rb:29:in `chdir'
      ./spec/spec/runner/formatter/html_formatter_spec.rb:29:

      29
      30  it "has a bug we need to fix" do
      31    pending "here is the bug" do
      32      # Actually, no. It's fixed. This will fail because it passes :-)
      33      mock = mock("Bug")

Running specs with --diff
  should print diff of different strings

      expected: "RSpec is a\nbehaviour driven development\nframework for Ruby\n",
           got: "RSpec is a\nbehavior driven development\nframework for Ruby\n" (using ==)

       Diff:
      @@ -1,4 +1,4 @@
       RSpec is a
      -behaviour driven development
      +behavior driven development
       framework for Ruby

      examples/failing/diffing_spec.rb:13:
      spec/spec_helper.rb:42:in `run_with'
      ./spec/spec/runner/formatter/html_formatter_spec.rb:41:
      ./spec/spec/runner/formatter/html_formatter_spec.rb:29:in `chdir'
      ./spec/spec/runner/formatter/html_formatter_spec.rb:29:

      11framework for Ruby
      12EOF
      13    usa.should == uk
      14  end

  should print diff of different objects' pretty representation

      expected <Animal
      name=bob,
      species=tortoise
      >

           got <Animal
      name=bob,
      species=giraffe
      >

      (compared using eql?)

      examples/failing/diffing_spec.rb:34:
      spec/spec_helper.rb:42:in `run_with'
      ./spec/spec/runner/formatter/html_formatter_spec.rb:41:
      ./spec/spec/runner/formatter/html_formatter_spec.rb:29:in `chdir'
      ./spec/spec/runner/formatter/html_formatter_spec.rb:29:

      32    expected = Animal.new "bob", "giraffe"
      33    actual   = Animal.new "bob", "tortoise"
      34    expected.should eql(actual)
      35  end
      36end

A consumer of a stub
  should be able to stub methods on any Object

A stubbed method on a class
  should return the stubbed value
  should revert to the original method after each spec
  can stub! and mock the same message

A mock
  can stub!
  can stub! and mock
  can stub! and mock the same message

pending example (using pending method)
  should be reported as "PENDING: for some reason" (PENDING: for some reason)

pending example (with no block)
  should be reported as "PENDING: Not Yet Implemented" (PENDING: Not Yet Implemented)

pending example (with block for pending)
  should have a failing block, passed to pending, reported as "PENDING: for some reason" (PENDING: for some reason)