# 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

      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/examples/failing/mocking_example.rb:13 :in `block (2 levels) in <top (required)>'
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:49 :in `block (4 levels) in '
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:45 :in `chdir'
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:45 :in `block (3 levels) in '

      11  it "should fail when expected message not received" do
      12    mock = mock("poke me")
      13    mock.should_receive(:poke)
      14  end
      15
      16# gem install syntax to get syntax highlighting

  should fail when messages are received out of order

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

      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/examples/failing/mocking_example.rb:22 :in `block (2 levels) in <top (required)>'
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:49 :in `block (4 levels) in '
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:45 :in `chdir'
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:45 :in `block (3 levels) in '

      20    mock.should_receive(:three).ordered
      21    mock.one
      22    mock.three
      23    mock.two
      24  end
      25# gem install syntax to get syntax highlighting

  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

      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/examples/failing/mocking_example.rb:29 :in `block (2 levels) in <top (required)>'
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:49 :in `block (4 levels) in '
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:45 :in `chdir'
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:45 :in `block (3 levels) in '

      27    mock = mock("don't talk to me")
      28    mock.should_not_receive(:any_message_at_all)
      29    mock.any_message_at_all
      30  end
      31
      32# gem install syntax to get syntax highlighting

  has a bug we need to fix

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

      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/examples/failing/mocking_example.rb:33 :in `block (2 levels) in <top (required)>'
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:49 :in `block (4 levels) in '
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:45 :in `chdir'
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:45 :in `block (3 levels) in '

      31
      32  it "has a bug we need to fix" do
      33    pending "here is the bug" do
      34      # Actually, no. It's fixed. This will fail because it passes :-)
      35      mock = mock("Bug")
      36# gem install syntax to get syntax highlighting

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

      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/examples/failing/diffing_spec.rb:13 :in `block (2 levels) in <top (required)>'
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:49 :in `block (4 levels) in '
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:45 :in `chdir'
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:45 :in `block (3 levels) in '

      11framework for Ruby
      12EOF
      13    usa.should == uk
      14  end
      15
      16# gem install syntax to get syntax highlighting

  should print diff of different objects' pretty representation

      expected <Animal
      name=bob,
      species=tortoise
      >

           got <Animal
      name=bob,
      species=giraffe
      >

      (compared using eql?)

      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/examples/failing/diffing_spec.rb:34 :in `block (2 levels) in <top (required)>'
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:49 :in `block (4 levels) in '
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:45 :in `chdir'
      /Users/david/projects/ruby/rspec-dev/example_rails_app/vendor/plugins/rspec/spec/spec/runner/formatter/text_mate_formatter_spec.rb:45 :in `block (3 levels) in '

      32    expected = Animal.new "bob", "giraffe"
      33    actual   = Animal.new "bob", "tortoise"
      34    expected.should eql(actual)
      35  end
      36end
      37# gem install syntax to get syntax highlighting

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)