changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/spec/spec/mocks/multiple_return_value_spec.rb

changeset 15: 64acf98d15f4
author: moriq@moriq.com
date: Mon Mar 10 10:12:58 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec
1require File.dirname(__FILE__) + '/../../spec_helper'
2
3module Spec
4 module Mocks
5 describe "a Mock expectation with multiple return values and no specified count" do
6 before(:each) do
7 @mock = Mock.new("mock")
8 @return_values = ["1",2,Object.new]
9 @mock.should_receive(:message).and_return(@return_values[0],@return_values[1],@return_values[2])
10 end
11
12 it "should return values in order to consecutive calls" do
13 @mock.message.should == @return_values[0]
14 @mock.message.should == @return_values[1]
15 @mock.message.should == @return_values[2]
16 @mock.rspec_verify
17 end
18
19 it "should complain when there are too few calls" do
20 third = Object.new
21 @mock.message.should == @return_values[0]
22 @mock.message.should == @return_values[1]
23 lambda { @mock.rspec_verify }.should raise_error(MockExpectationError, "Mock 'mock' expected :message with (any args) 3 times, but received it twice")
24 end
25
26 it "should complain when there are too many calls" do
27 third = Object.new
28 @mock.message.should == @return_values[0]
29 @mock.message.should == @return_values[1]
30 @mock.message.should == @return_values[2]
31 @mock.message.should == @return_values[2]
32 lambda { @mock.rspec_verify }.should raise_error(MockExpectationError, "Mock 'mock' expected :message with (any args) 3 times, but received it 4 times")
33 end
34 end
35
36 describe "a Mock expectation with multiple return values with a specified count equal to the number of values" do
37 before(:each) do
38 @mock = Mock.new("mock")
39 @return_values = ["1",2,Object.new]
40 @mock.should_receive(:message).exactly(3).times.and_return(@return_values[0],@return_values[1],@return_values[2])
41 end
42
43 it "should return values in order to consecutive calls" do
44 @mock.message.should == @return_values[0]
45 @mock.message.should == @return_values[1]
46 @mock.message.should == @return_values[2]
47 @mock.rspec_verify
48 end
49
50 it "should complain when there are too few calls" do
51 third = Object.new
52 @mock.message.should == @return_values[0]
53 @mock.message.should == @return_values[1]
54 lambda { @mock.rspec_verify }.should raise_error(MockExpectationError, "Mock 'mock' expected :message with (any args) 3 times, but received it twice")
55 end
56
57 it "should complain when there are too many calls" do
58 third = Object.new
59 @mock.message.should == @return_values[0]
60 @mock.message.should == @return_values[1]
61 @mock.message.should == @return_values[2]
62 @mock.message.should == @return_values[2]
63 lambda { @mock.rspec_verify }.should raise_error(MockExpectationError, "Mock 'mock' expected :message with (any args) 3 times, but received it 4 times")
64 end
65 end
66
67 describe "a Mock expectation with multiple return values specifying at_least less than the number of values" do
68 before(:each) do
69 @mock = Mock.new("mock")
70 @mock.should_receive(:message).at_least(:twice).with(no_args).and_return(11, 22)
71 end
72
73 it "should use last return value for subsequent calls" do
74 @mock.message.should equal(11)
75 @mock.message.should equal(22)
76 @mock.message.should equal(22)
77 @mock.rspec_verify
78 end
79
80 it "should fail when called less than the specified number" do
81 @mock.message.should equal(11)
82 lambda { @mock.rspec_verify }.should raise_error(MockExpectationError, "Mock 'mock' expected :message with (no args) twice, but received it once")
83 end
84 end
85 describe "a Mock expectation with multiple return values with a specified count larger than the number of values" do
86 before(:each) do
87 @mock = Mock.new("mock")
88 @mock.should_receive(:message).exactly(3).times.and_return(11, 22)
89 end
90
91 it "should use last return value for subsequent calls" do
92 @mock.message.should equal(11)
93 @mock.message.should equal(22)
94 @mock.message.should equal(22)
95 @mock.rspec_verify
96 end
97
98 it "should fail when called less than the specified number" do
99 @mock.message.should equal(11)
100 lambda { @mock.rspec_verify }.should raise_error(MockExpectationError, "Mock 'mock' expected :message with (any args) 3 times, but received it once")
101 end
102
103 it "should fail when called greater than the specified number" do
104 @mock.message.should equal(11)
105 @mock.message.should equal(22)
106 @mock.message.should equal(22)
107 @mock.message.should equal(22)
108 lambda { @mock.rspec_verify }.should raise_error(MockExpectationError, "Mock 'mock' expected :message with (any args) 3 times, but received it 4 times")
109 end
110 end
111 end
112end
113