]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/examples/pure/stubbing_example.rb
fix node relations bug (thanks Dave!)
[rails.git] / vendor / gems / rspec-1.1.2 / examples / pure / stubbing_example.rb
1 require File.dirname(__FILE__) + '/spec_helper'
2
3 describe "A consumer of a stub" do
4   it "should be able to stub methods on any Object" do
5     obj = Object.new
6     obj.stub!(:foobar).and_return {:return_value}
7     obj.foobar.should equal(:return_value)
8   end
9 end
10
11 class StubbableClass
12   def self.find id
13     return :original_return
14   end
15 end
16
17 describe "A stubbed method on a class" do
18   it "should return the stubbed value" do
19     StubbableClass.stub!(:find).and_return(:stub_return)
20     StubbableClass.find(1).should equal(:stub_return)
21   end
22   
23   it "should revert to the original method after each spec" do
24     StubbableClass.find(1).should equal(:original_return)
25   end
26
27   it "can stub! and mock the same message" do
28     StubbableClass.stub!(:msg).and_return(:stub_value)
29     StubbableClass.should_receive(:msg).with(:arg).and_return(:mock_value)
30
31     StubbableClass.msg.should equal(:stub_value)
32     StubbableClass.msg(:other_arg).should equal(:stub_value)
33     StubbableClass.msg(:arg).should equal(:mock_value)
34     StubbableClass.msg(:another_arg).should equal(:stub_value)
35     StubbableClass.msg(:yet_another_arg).should equal(:stub_value)
36     StubbableClass.msg.should equal(:stub_value)
37   end
38 end
39
40 describe "A mock" do
41   it "can stub!" do
42     mock = mock("stubbing mock")
43     mock.stub!(:msg).and_return(:value)
44     (1..10).each {mock.msg.should equal(:value)}
45   end
46   
47   it "can stub! and mock" do
48     mock = mock("stubbing mock")
49     mock.stub!(:stub_message).and_return(:stub_value)
50     mock.should_receive(:mock_message).once.and_return(:mock_value)
51     (1..10).each {mock.stub_message.should equal(:stub_value)}
52     mock.mock_message.should equal(:mock_value)
53     (1..10).each {mock.stub_message.should equal(:stub_value)}
54   end
55   
56   it "can stub! and mock the same message" do
57     mock = mock("stubbing mock")
58     mock.stub!(:msg).and_return(:stub_value)
59     mock.should_receive(:msg).with(:arg).and_return(:mock_value)
60     mock.msg.should equal(:stub_value)
61     mock.msg(:other_arg).should equal(:stub_value)
62     mock.msg(:arg).should equal(:mock_value)
63     mock.msg(:another_arg).should equal(:stub_value)
64     mock.msg(:yet_another_arg).should equal(:stub_value)
65     mock.msg.should equal(:stub_value)
66   end
67 end
68
69