1 require File.dirname(__FILE__) + '/../../spec_helper.rb'
4 def create_collection_owner_with(n)
5 owner = Spec::Expectations::Helper::CollectionOwner.new
7 owner.add_to_collection_with_length_method(n)
8 owner.add_to_collection_with_size_method(n)
14 describe "should have(n).items" do
15 include HaveSpecHelper
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
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
29 it "should fail if target has a collection of items with < n members" do
30 owner = create_collection_owner_with(3)
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")
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")
39 it "should fail if target has a collection of items with > n members" do
40 owner = create_collection_owner_with(3)
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")
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")
50 describe 'should have(1).item when Inflector is defined' do
51 include HaveSpecHelper
54 unless Object.const_defined?(:Inflector)
56 def self.pluralize(string)
63 it 'should pluralize the collection name' do
64 owner = create_collection_owner_with(1)
65 owner.should have(1).item
69 describe "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
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")
82 describe "should_not have(n).items" do
83 include HaveSpecHelper
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
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
97 it "should fail if target has a collection of items with n members" do
98 owner = create_collection_owner_with(3)
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")
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")
108 describe "should have_exactly(n).items" do
109 include HaveSpecHelper
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
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
123 it "should fail if target has a collection of items with < n members" do
124 owner = create_collection_owner_with(3)
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")
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")
133 it "should fail if target has a collection of items with > n members" do
134 owner = create_collection_owner_with(3)
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")
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")
144 describe "should have_at_least(n).items" do
145 include HaveSpecHelper
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
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
159 it "should fail if target has a collection of items with < n members" do
160 owner = create_collection_owner_with(3)
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")
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")
169 it "should provide educational negative failure messages" do
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
176 length_matcher.matches?(owner)
177 size_matcher.matches?(owner)
180 length_matcher.negative_failure_message.should == <<-EOF
181 Isn't life confusing enough?
182 Instead of having to figure out the meaning of this:
183 should_not have_at_least(3).items_in_collection_with_length_method
184 We recommend that you use this instead:
185 should have_at_most(2).items_in_collection_with_length_method
188 size_matcher.negative_failure_message.should == <<-EOF
189 Isn't life confusing enough?
190 Instead of having to figure out the meaning of this:
191 should_not have_at_least(3).items_in_collection_with_size_method
192 We recommend that you use this instead:
193 should have_at_most(2).items_in_collection_with_size_method
198 describe "should have_at_most(n).items" do
199 include HaveSpecHelper
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
207 it "should fail if target has a collection of items with > n members" do
208 owner = create_collection_owner_with(3)
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")
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")
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
223 it "should provide educational negative failure messages" do
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
230 length_matcher.matches?(owner)
231 size_matcher.matches?(owner)
234 length_matcher.negative_failure_message.should == <<-EOF
235 Isn't life confusing enough?
236 Instead of having to figure out the meaning of this:
237 should_not have_at_most(3).items_in_collection_with_length_method
238 We recommend that you use this instead:
239 should have_at_least(4).items_in_collection_with_length_method
242 size_matcher.negative_failure_message.should == <<-EOF
243 Isn't life confusing enough?
244 Instead of having to figure out the meaning of this:
245 should_not have_at_most(3).items_in_collection_with_size_method
246 We recommend that you use this instead:
247 should have_at_least(4).items_in_collection_with_size_method
252 describe "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")
259 it "should pass block to target" do
260 target = mock("target")
262 target.should_receive(:items).with("arg1","arg2", block).and_return([1,2,3])
263 target.should have(3).items("arg1","arg2", block)
267 describe "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
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")
277 describe "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
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")
287 describe "have(n).things on an object which is not a collection nor contains one" do
289 lambda { Object.new.should have(2).things }.should raise_error(NoMethodError, /undefined method `things' for #<Object:/)