changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/spec/spec/example/shared_example_group_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 Example
5 describe ExampleGroup, "with :shared => true" do
6 it_should_behave_like "sandboxed rspec_options"
7 attr_reader :formatter, :example_group
8 before(:each) do
9 @formatter = Spec::Mocks::Mock.new("formatter", :null_object => true)
10 options.formatters << formatter
11 @example_group = Class.new(ExampleGroup).describe("example_group")
12 class << example_group
13 public :include
14 end
15 end
16
17 after(:each) do
18 @formatter.rspec_verify
19 @example_group = nil
20 $shared_example_groups.clear unless $shared_example_groups.nil?
21 end
22
23 def make_shared_example_group(name, opts=nil, &block)
24 example_group = SharedExampleGroup.new(name, :shared => true, &block)
25 SharedExampleGroup.add_shared_example_group(example_group)
26 example_group
27 end
28
29 def non_shared_example_group()
30 @non_shared_example_group ||= Class.new(ExampleGroup).describe("example_group")
31 end
32
33 it "should accept an optional options hash" do
34 lambda { Class.new(ExampleGroup).describe("context") }.should_not raise_error(Exception)
35 lambda { Class.new(ExampleGroup).describe("context", :shared => true) }.should_not raise_error(Exception)
36 end
37
38 it "should return all shared example_groups" do
39 b1 = make_shared_example_group("b1", :shared => true) {}
40 b2 = make_shared_example_group("b2", :shared => true) {}
41
42 b1.should_not be(nil)
43 b2.should_not be(nil)
44
45 SharedExampleGroup.find_shared_example_group("b1").should equal(b1)
46 SharedExampleGroup.find_shared_example_group("b2").should equal(b2)
47 end
48
49 it "should register as shared example_group" do
50 example_group = make_shared_example_group("example_group") {}
51 SharedExampleGroup.shared_example_groups.should include(example_group)
52 end
53
54 it "should not be shared when not configured as shared" do
55 example_group = non_shared_example_group
56 SharedExampleGroup.shared_example_groups.should_not include(example_group)
57 end
58
59 it "should complain when adding a second shared example_group with the same description" do
60 describe "shared example_group", :shared => true do
61 end
62 lambda do
63 describe "shared example_group", :shared => true do
64 end
65 end.should raise_error(ArgumentError)
66 end
67
68 it "should NOT complain when adding the same shared example_group instance again" do
69 shared_example_group = Class.new(ExampleGroup).describe("shared example_group", :shared => true)
70 SharedExampleGroup.add_shared_example_group(shared_example_group)
71 SharedExampleGroup.add_shared_example_group(shared_example_group)
72 end
73
74 it "should NOT complain when adding the same shared example_group again (i.e. file gets reloaded)" do
75 lambda do
76 2.times do
77 describe "shared example_group which gets loaded twice", :shared => true do
78 end
79 end
80 end.should_not raise_error(ArgumentError)
81 end
82
83 it "should NOT complain when adding the same shared example_group in same file with different absolute path" do
84 shared_example_group_1 = Class.new(ExampleGroup).describe(
85 "shared example_group",
86 :shared => true,
87 :spec_path => "/my/spec/a/../shared.rb"
88 )
89 shared_example_group_2 = Class.new(ExampleGroup).describe(
90 "shared example_group",
91 :shared => true,
92 :spec_path => "/my/spec/b/../shared.rb"
93 )
94
95 SharedExampleGroup.add_shared_example_group(shared_example_group_1)
96 SharedExampleGroup.add_shared_example_group(shared_example_group_2)
97 end
98
99 it "should complain when adding a different shared example_group with the same name in a different file with the same basename" do
100 shared_example_group_1 = Class.new(ExampleGroup).describe(
101 "shared example_group",
102 :shared => true,
103 :spec_path => "/my/spec/a/shared.rb"
104 )
105 shared_example_group_2 = Class.new(ExampleGroup).describe(
106 "shared example_group",
107 :shared => true,
108 :spec_path => "/my/spec/b/shared.rb"
109 )
110
111 SharedExampleGroup.add_shared_example_group(shared_example_group_1)
112 lambda do
113 SharedExampleGroup.add_shared_example_group(shared_example_group_2)
114 end.should raise_error(ArgumentError, /already exists/)
115 end
116
117 it "should add examples to current example_group using it_should_behave_like" do
118 shared_example_group = make_shared_example_group("shared example_group") do
119 it("shared example") {}
120 it("shared example 2") {}
121 end
122
123 example_group.it("example") {}
124 example_group.number_of_examples.should == 1
125 example_group.it_should_behave_like("shared example_group")
126 example_group.number_of_examples.should == 3
127 end
128
129 it "should add examples to current example_group using include" do
130 shared_example_group = describe "all things", :shared => true do
131 it "should do stuff" do end
132 end
133
134 example_group = describe "one thing" do
135 include shared_example_group
136 end
137
138 example_group.number_of_examples.should == 1
139 end
140
141 it "should add examples to current example_group using it_should_behave_like with a module" do
142 AllThings = describe "all things", :shared => true do
143 it "should do stuff" do end
144 end
145
146 example_group = describe "one thing" do
147 it_should_behave_like AllThings
148 end
149
150 example_group.number_of_examples.should == 1
151 end
152
153 it "should run shared examples" do
154 shared_example_ran = false
155 shared_example_group = make_shared_example_group("shared example_group") do
156 it("shared example") { shared_example_ran = true }
157 end
158
159 example_ran = false
160
161 example_group.it_should_behave_like("shared example_group")
162 example_group.it("example") {example_ran = true}
163 example_group.run
164 example_ran.should be_true
165 shared_example_ran.should be_true
166 end
167
168 it "should run setup and teardown from shared example_group" do
169 shared_setup_ran = false
170 shared_teardown_ran = false
171 shared_example_group = make_shared_example_group("shared example_group") do
172 before { shared_setup_ran = true }
173 after { shared_teardown_ran = true }
174 it("shared example") { shared_example_ran = true }
175 end
176
177 example_ran = false
178
179 example_group.it_should_behave_like("shared example_group")
180 example_group.it("example") {example_ran = true}
181 example_group.run
182 example_ran.should be_true
183 shared_setup_ran.should be_true
184 shared_teardown_ran.should be_true
185 end
186
187 it "should run before(:all) and after(:all) only once from shared example_group" do
188 shared_before_all_run_count = 0
189 shared_after_all_run_count = 0
190 shared_example_group = make_shared_example_group("shared example_group") do
191 before(:all) { shared_before_all_run_count += 1}
192 after(:all) { shared_after_all_run_count += 1}
193 it("shared example") { shared_example_ran = true }
194 end
195
196 example_ran = false
197
198 example_group.it_should_behave_like("shared example_group")
199 example_group.it("example") {example_ran = true}
200 example_group.run
201 example_ran.should be_true
202 shared_before_all_run_count.should == 1
203 shared_after_all_run_count.should == 1
204 end
205
206 it "should include modules, included into shared example_group, into current example_group" do
207 @formatter.should_receive(:add_example_group).with(any_args)
208
209 shared_example_group = make_shared_example_group("shared example_group") do
210 it("shared example") { shared_example_ran = true }
211 end
212
213 mod1_method_called = false
214 mod1 = Module.new do
215 define_method :mod1_method do
216 mod1_method_called = true
217 end
218 end
219
220 mod2_method_called = false
221 mod2 = Module.new do
222 define_method :mod2_method do
223 mod2_method_called = true
224 end
225 end
226
227 shared_example_group.include mod2
228
229 example_group.it_should_behave_like("shared example_group")
230 example_group.include mod1
231
232 example_group.it("test") do
233 mod1_method
234 mod2_method
235 end
236 example_group.run
237 mod1_method_called.should be_true
238 mod2_method_called.should be_true
239 end
240
241 it "should make methods defined in the shared example_group available in consuming example_group" do
242 shared_example_group = make_shared_example_group("shared example_group xyz") do
243 def a_shared_helper_method
244 "this got defined in a shared example_group"
245 end
246 end
247 example_group.it_should_behave_like("shared example_group xyz")
248 success = false
249 example_group.it("should access a_shared_helper_method") do
250 a_shared_helper_method
251 success = true
252 end
253 example_group.run
254 success.should be_true
255 end
256
257 it "should raise when named shared example_group can not be found" do
258 lambda {
259 example_group.it_should_behave_like("non-existent shared example group")
260 violated
261 }.should raise_error("Shared Example Group 'non-existent shared example group' can not be found")
262 end
263 end
264 end
265end