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
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
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
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.
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
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
>
 (using .eql?)
Diff:
@@ -1,5 +1,5 @@
 <Animal
 name=bob,
-species=tortoise
+species=giraffe
 >
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)