changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/spec/spec/matchers/have_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.rb'
2
3module HaveSpecHelper
4 def create_collection_owner_with(n)
5 owner = Spec::Expectations::Helper::CollectionOwner.new
6 (1..n).each do |n|
7 owner.add_to_collection_with_length_method(n)
8 owner.add_to_collection_with_size_method(n)
9 end
10 owner
11 end
12end
13
14describe "should have(n).items" do
15 include HaveSpecHelper
16
17 it "should pass if target has a collection of items with n members" do
18 owner = create_collection_owner_with(3)
19 owner.should have(3).items_in_collection_with_length_method
20 owner.should have(3).items_in_collection_with_size_method
21 end
22
23 it "should convert :no to 0" do
24 owner = create_collection_owner_with(0)
25 owner.should have(:no).items_in_collection_with_length_method
26 owner.should have(:no).items_in_collection_with_size_method
27 end
28
29 it "should fail if target has a collection of items with < n members" do
30 owner = create_collection_owner_with(3)
31 lambda {
32 owner.should have(4).items_in_collection_with_length_method
33 }.should fail_with("expected 4 items_in_collection_with_length_method, got 3")
34 lambda {
35 owner.should have(4).items_in_collection_with_size_method
36 }.should fail_with("expected 4 items_in_collection_with_size_method, got 3")
37 end
38
39 it "should fail if target has a collection of items with > n members" do
40 owner = create_collection_owner_with(3)
41 lambda {
42 owner.should have(2).items_in_collection_with_length_method
43 }.should fail_with("expected 2 items_in_collection_with_length_method, got 3")
44 lambda {
45 owner.should have(2).items_in_collection_with_size_method
46 }.should fail_with("expected 2 items_in_collection_with_size_method, got 3")
47 end
48end
49
50describe 'should have(1).item when Inflector is defined' do
51 include HaveSpecHelper
52
53 before do
54 unless Object.const_defined?(:Inflector)
55 class Inflector
56 def self.pluralize(string)
57 string.to_s + 's'
58 end
59 end
60 end
61 end
62
63 it 'should pluralize the collection name' do
64 owner = create_collection_owner_with(1)
65 owner.should have(1).item
66 end
67end
68
69describe "should have(n).items where result responds to items but returns something other than a collection" do
70 it "should provide a meaningful error" do
71 owner = Class.new do
72 def items
73 Object.new
74 end
75 end.new
76 lambda do
77 owner.should have(3).items
78 end.should raise_error("expected items to be a collection but it does not respond to #length or #size")
79 end
80end
81
82describe "should_not have(n).items" do
83 include HaveSpecHelper
84
85 it "should pass if target has a collection of items with < n members" do
86 owner = create_collection_owner_with(3)
87 owner.should_not have(4).items_in_collection_with_length_method
88 owner.should_not have(4).items_in_collection_with_size_method
89 end
90
91 it "should pass if target has a collection of items with > n members" do
92 owner = create_collection_owner_with(3)
93 owner.should_not have(2).items_in_collection_with_length_method
94 owner.should_not have(2).items_in_collection_with_size_method
95 end
96
97 it "should fail if target has a collection of items with n members" do
98 owner = create_collection_owner_with(3)
99 lambda {
100 owner.should_not have(3).items_in_collection_with_length_method
101 }.should fail_with("expected target not to have 3 items_in_collection_with_length_method, got 3")
102 lambda {
103 owner.should_not have(3).items_in_collection_with_size_method
104 }.should fail_with("expected target not to have 3 items_in_collection_with_size_method, got 3")
105 end
106end
107
108describe "should have_exactly(n).items" do
109 include HaveSpecHelper
110
111 it "should pass if target has a collection of items with n members" do
112 owner = create_collection_owner_with(3)
113 owner.should have_exactly(3).items_in_collection_with_length_method
114 owner.should have_exactly(3).items_in_collection_with_size_method
115 end
116
117 it "should convert :no to 0" do
118 owner = create_collection_owner_with(0)
119 owner.should have_exactly(:no).items_in_collection_with_length_method
120 owner.should have_exactly(:no).items_in_collection_with_size_method
121 end
122
123 it "should fail if target has a collection of items with < n members" do
124 owner = create_collection_owner_with(3)
125 lambda {
126 owner.should have_exactly(4).items_in_collection_with_length_method
127 }.should fail_with("expected 4 items_in_collection_with_length_method, got 3")
128 lambda {
129 owner.should have_exactly(4).items_in_collection_with_size_method
130 }.should fail_with("expected 4 items_in_collection_with_size_method, got 3")
131 end
132
133 it "should fail if target has a collection of items with > n members" do
134 owner = create_collection_owner_with(3)
135 lambda {
136 owner.should have_exactly(2).items_in_collection_with_length_method
137 }.should fail_with("expected 2 items_in_collection_with_length_method, got 3")
138 lambda {
139 owner.should have_exactly(2).items_in_collection_with_size_method
140 }.should fail_with("expected 2 items_in_collection_with_size_method, got 3")
141 end
142end
143
144describe "should have_at_least(n).items" do
145 include HaveSpecHelper
146
147 it "should pass if target has a collection of items with n members" do
148 owner = create_collection_owner_with(3)
149 owner.should have_at_least(3).items_in_collection_with_length_method
150 owner.should have_at_least(3).items_in_collection_with_size_method
151 end
152
153 it "should pass if target has a collection of items with > n members" do
154 owner = create_collection_owner_with(3)
155 owner.should have_at_least(2).items_in_collection_with_length_method
156 owner.should have_at_least(2).items_in_collection_with_size_method
157 end
158
159 it "should fail if target has a collection of items with < n members" do
160 owner = create_collection_owner_with(3)
161 lambda {
162 owner.should have_at_least(4).items_in_collection_with_length_method
163 }.should fail_with("expected at least 4 items_in_collection_with_length_method, got 3")
164 lambda {
165 owner.should have_at_least(4).items_in_collection_with_size_method
166 }.should fail_with("expected at least 4 items_in_collection_with_size_method, got 3")
167 end
168
169 it "should provide educational negative failure messages" do
170 #given
171 owner = create_collection_owner_with(3)
172 length_matcher = have_at_least(3).items_in_collection_with_length_method
173 size_matcher = have_at_least(3).items_in_collection_with_size_method
174
175 #when
176 length_matcher.matches?(owner)
177 size_matcher.matches?(owner)
178
179 #then
180 length_matcher.negative_failure_message.should == <<-EOF
181Isn't life confusing enough?
182Instead of having to figure out the meaning of this:
183 should_not have_at_least(3).items_in_collection_with_length_method
184We recommend that you use this instead:
185 should have_at_most(2).items_in_collection_with_length_method
186EOF
187
188 size_matcher.negative_failure_message.should == <<-EOF
189Isn't life confusing enough?
190Instead of having to figure out the meaning of this:
191 should_not have_at_least(3).items_in_collection_with_size_method
192We recommend that you use this instead:
193 should have_at_most(2).items_in_collection_with_size_method
194EOF
195 end
196end
197
198describe "should have_at_most(n).items" do
199 include HaveSpecHelper
200
201 it "should pass if target has a collection of items with n members" do
202 owner = create_collection_owner_with(3)
203 owner.should have_at_most(3).items_in_collection_with_length_method
204 owner.should have_at_most(3).items_in_collection_with_size_method
205 end
206
207 it "should fail if target has a collection of items with > n members" do
208 owner = create_collection_owner_with(3)
209 lambda {
210 owner.should have_at_most(2).items_in_collection_with_length_method
211 }.should fail_with("expected at most 2 items_in_collection_with_length_method, got 3")
212 lambda {
213 owner.should have_at_most(2).items_in_collection_with_size_method
214 }.should fail_with("expected at most 2 items_in_collection_with_size_method, got 3")
215 end
216
217 it "should pass if target has a collection of items with < n members" do
218 owner = create_collection_owner_with(3)
219 owner.should have_at_most(4).items_in_collection_with_length_method
220 owner.should have_at_most(4).items_in_collection_with_size_method
221 end
222
223 it "should provide educational negative failure messages" do
224 #given
225 owner = create_collection_owner_with(3)
226 length_matcher = have_at_most(3).items_in_collection_with_length_method
227 size_matcher = have_at_most(3).items_in_collection_with_size_method
228
229 #when
230 length_matcher.matches?(owner)
231 size_matcher.matches?(owner)
232
233 #then
234 length_matcher.negative_failure_message.should == <<-EOF
235Isn't life confusing enough?
236Instead of having to figure out the meaning of this:
237 should_not have_at_most(3).items_in_collection_with_length_method
238We recommend that you use this instead:
239 should have_at_least(4).items_in_collection_with_length_method
240EOF
241
242 size_matcher.negative_failure_message.should == <<-EOF
243Isn't life confusing enough?
244Instead of having to figure out the meaning of this:
245 should_not have_at_most(3).items_in_collection_with_size_method
246We recommend that you use this instead:
247 should have_at_least(4).items_in_collection_with_size_method
248EOF
249 end
250end
251
252describe "have(n).items(args, block)" do
253 it "should pass args to target" do
254 target = mock("target")
255 target.should_receive(:items).with("arg1","arg2").and_return([1,2,3])
256 target.should have(3).items("arg1","arg2")
257 end
258
259 it "should pass block to target" do
260 target = mock("target")
261 block = lambda { 5 }
262 target.should_receive(:items).with("arg1","arg2", block).and_return([1,2,3])
263 target.should have(3).items("arg1","arg2", block)
264 end
265end
266
267describe "have(n).items where target IS a collection" do
268 it "should reference the number of items IN the collection" do
269 [1,2,3].should have(3).items
270 end
271
272 it "should fail when the number of items IN the collection is not as expected" do
273 lambda { [1,2,3].should have(7).items }.should fail_with("expected 7 items, got 3")
274 end
275end
276
277describe "have(n).characters where target IS a String" do
278 it "should pass if the length is correct" do
279 "this string".should have(11).characters
280 end
281
282 it "should fail if the length is incorrect" do
283 lambda { "this string".should have(12).characters }.should fail_with("expected 12 characters, got 11")
284 end
285end
286
287describe "have(n).things on an object which is not a collection nor contains one" do
288 it "should fail" do
289 lambda { Object.new.should have(2).things }.should raise_error(NoMethodError, /undefined method `things' for #<Object:/)
290 end
291end