scenario 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,8 @@ Scenario
3
3
 
4
4
  Scenarios is a super-simple way to move common setup and helper methods into cohesive "scenarios" that can be included easily in your specs. The advantage of a scenario over regular mixin modules is that you can use RSpec hooks (eg. `before :each {}`) to do the setup instead of having redundant, copy-pasted setup littering your specs.
5
5
 
6
+ Scenario also includes a basic DSL for performing setup atomically. That is, instead of doing expensive setup in a `before :all` block that is always run, you can run only the setup that you need in a given `describe` with the Scenario DSL.
7
+
6
8
  Examples
7
9
  --------
8
10
 
@@ -96,6 +98,77 @@ describe "GET /message/new" do
96
98
  end
97
99
  ```
98
100
 
101
+ Setup
102
+ -----
103
+
104
+ Setup is executed `before :all`.
105
+
106
+ ```ruby
107
+ describe_scenario :user do
108
+ setup_for :user do |name, password|
109
+ name ||= "Bob"
110
+ password ||= "foo"
111
+ @user = User.create( :name => name, :password => password )
112
+ end
113
+
114
+ setup_for :admin do |name, password|
115
+ name ||= "Sally"
116
+ password ||= "bar"
117
+ @user = User.create( :name => name, :password => password, :admin => true )
118
+ end
119
+
120
+ def login
121
+ visit( url( :login ) )
122
+ fill_in :name, :with => "Bob"
123
+ fill_in :password, :with => "foo"
124
+ click_button "Login"
125
+ response
126
+ end
127
+ end
128
+
129
+ # Pass a hash:
130
+
131
+ describe "One user" do
132
+ scenario :user, :setup => :user
133
+
134
+ it "has one user" do
135
+ User.count.should == 1
136
+ end
137
+ end
138
+
139
+ describe "Two users" do
140
+ scenario :user, :setup => [:user, :admin]
141
+
142
+ it "has two users" do
143
+ User.count.should == 2
144
+ end
145
+ end
146
+
147
+ # Or pass a block:
148
+
149
+ describe "One user" do
150
+ scenario :user do
151
+ setup_user( "Sam" )
152
+ end
153
+
154
+ it "has one user" do
155
+ User.count.should == 1
156
+ end
157
+ end
158
+
159
+ # Or both!
160
+
161
+ describe "Two users" do
162
+ scenario :user, :setup => :user do
163
+ setup_admin( "Steph" )
164
+ end
165
+
166
+ it "has two users" do
167
+ User.count.should == 2
168
+ end
169
+ end
170
+ ```
171
+
99
172
  Fixtures
100
173
  --------
101
174
 
@@ -114,6 +187,7 @@ to be read once.
114
187
  Version History
115
188
  ===============
116
189
 
190
+ * 0.3.1 - Add "setup" syntax.
117
191
  * 0.3.0 - Remove support for module-based scenarios, allow calling RSpec hooks
118
192
  in scenarios.
119
193
  * 0.2.0 - Add support for RSpec 1.3+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.1
@@ -20,10 +20,26 @@ module Scenario
20
20
  module ExampleGroupExtensions
21
21
 
22
22
  # Load a given scenario
23
- def scenario( name, &block )
23
+ def scenario( name, opts={}, &block )
24
24
  @_scenario_context = name.to_sym
25
25
  scenario = Scenario::Scenarios.for( name )
26
+
27
+ # Load scenario
26
28
  self.module_eval( &scenario )
29
+
30
+ # Set up
31
+ Array( opts[:setup] ).each do |subject|
32
+ self.module_eval do
33
+ before :all do
34
+ self.send "setup_#{subject}"
35
+ end
36
+ end
37
+ end
38
+ self.module_eval { before( :all, &block ) } if block_given?
39
+ end
40
+
41
+ def setup_for( name, &block )
42
+ self.send( :define_method, "setup_#{name}", block )
27
43
  end
28
44
 
29
45
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{scenario}
8
- s.version = "0.3.0"
8
+ s.version = "0.3.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tyson Tate"]
12
- s.date = %q{2011-05-21}
12
+ s.date = %q{2011-05-23}
13
13
  s.description = %q{Basic spec scenarios for RSpec. Also includes basic fixtures support.}
14
14
  s.email = %q{tyson@tysontate.com}
15
15
  s.extra_rdoc_files = [
@@ -4,4 +4,8 @@ describe Scenario::Fixtures do
4
4
  it "provides text" do
5
5
  Scenario::Fixtures["basic.text"].should =~ /squeeze/
6
6
  end
7
+
8
+ it "bombs out on non-existant files" do
9
+ lambda { Scenario::Fixtures["lol.text"] }.should raise_error( RuntimeError )
10
+ end
7
11
  end
@@ -1,57 +1,170 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
  require "scenarios/test_scenarios"
3
3
 
4
- describe "Example with one scenario" do
4
+ # =========
5
+ # = Basic =
6
+ # =========
7
+
8
+ describe "describe with one scenario" do
5
9
  scenario :simple
6
10
 
7
11
  it "should have a scenario" do
8
- @zomg_all.should( be_true )
9
- @zomg_each.should( be_true )
10
- lol.should == "lol"
12
+ @scenario_all.should( be_true )
13
+ @scenario_each.should( be_true )
14
+ let_block.should == 1
15
+ scenario_method.should == "present"
11
16
  end
12
17
  end
13
18
 
14
- describe "Example with multiple scenarios" do
19
+ describe "describe with multiple scenarios" do
15
20
  scenario :scenario_one
16
21
  scenario :scenario_two
17
22
 
18
23
  it "should have both scenarios" do
19
24
  one.should == "present"
20
25
  two.should == "also present"
21
- zero_cool?.should be_true
26
+ scenario_method?.should be_true
27
+ end
28
+ end
29
+
30
+ describe "describe without a scenario" do
31
+ it "shouldn't have a scenario" do
32
+ @scenario_all.should be_nil
33
+ @scenario_each.should be_nil
34
+ lambda { lol }.should raise_error
22
35
  end
23
36
  end
24
37
 
25
- describe "Nested Examples" do
38
+ # ===========
39
+ # = Nesting =
40
+ # ===========
41
+
42
+ describe "Nested describes" do
26
43
  scenario :scenario_one
27
44
 
28
- it "has scenario methods, but not those of descendant examples" do
45
+ it "don't leak nested scenarios to the parent describe" do
29
46
  one.should == "present"
30
47
  lambda { two }.should raise_error
31
- zero_cool?.should be_false
48
+ scenario_method?.should be_false
49
+ @scenario_one_present.should be_true
50
+ @scenario_two_present.should be_nil
51
+ end
52
+
53
+ describe "in nested describe with an added scenario" do
54
+ scenario :scenario_two
55
+
56
+ it "has all scenarios" do
57
+ one.should == "present"
58
+ two.should == "also present"
59
+ scenario_method?.should be_true
60
+ @scenario_one_present.should be_true
61
+ @scenario_two_present.should be_true
62
+ end
32
63
  end
33
64
 
34
- describe "(one deeper)" do
65
+ context "in nested context with an added scenario" do
35
66
  scenario :scenario_two
36
67
 
37
- it "has scenario (local and parent example) methods" do
68
+ it "has all scenarios" do
38
69
  one.should == "present"
39
70
  two.should == "also present"
40
- zero_cool?.should be_true
71
+ scenario_method?.should be_true
72
+ @scenario_one_present.should be_true
73
+ @scenario_two_present.should be_true
41
74
  end
42
75
  end
43
76
 
44
- describe "(one deeper, without extra scenario)" do
45
- it "has parent scenario methods" do
77
+ describe "in nested example without an additional scenario" do
78
+ it "has parent scenario" do
46
79
  one.should == "present"
47
80
  lambda { two }.should raise_error
48
- zero_cool?.should be_false
81
+ scenario_method?.should be_false
82
+ @scenario_one_present.should be_true
83
+ @scenario_two_present.should be_nil
49
84
  end
50
85
  end
51
86
  end
52
87
 
53
- describe "Example with no scenario" do
54
- it "shouldn't have any scenario methods" do
55
- lambda { lol }.should raise_error
88
+ # ==========
89
+ # = Shared =
90
+ # ==========
91
+
92
+ shared_examples_for "without scenario in the shared group" do
93
+ it "should inherit scenario" do
94
+ @scenario_all.should( be_true )
95
+ @scenario_each.should( be_true )
96
+ let_block.should == 1
97
+ scenario_method.should == "present"
98
+ end
99
+ end
100
+
101
+ shared_examples_for "with scenario in the shared group" do
102
+ scenario :simple
103
+
104
+ it "should have a scenario" do
105
+ @scenario_all.should( be_true )
106
+ @scenario_each.should( be_true )
107
+ let_block.should == 1
108
+ scenario_method.should == "present"
109
+ end
110
+ end
111
+
112
+ describe "Shared example groups" do
113
+ context "without scenario in shared group" do
114
+ scenario :simple
115
+ it_should_behave_like "without scenario in the shared group"
116
+ end
117
+
118
+ context "with scenario in shared group" do
119
+ it_should_behave_like "with scenario in the shared group"
120
+
121
+ it "shouldn't leak" do
122
+ @scenario_all.should be_nil
123
+ @scenario_each.should be_nil
124
+ lambda { scenario_method }.should raise_error
125
+ end
126
+ end
127
+ end
128
+
129
+ # ==================
130
+ # = Scenario setup =
131
+ # ==================
132
+
133
+ describe "scenario with setup methods" do
134
+ describe "without calling setup" do
135
+ scenario :setup_scenario
136
+
137
+ it "shouldn't do any setup" do
138
+ thing1.should be_nil
139
+ end
140
+ end
141
+
142
+ describe "calling setup via opts hash" do
143
+ scenario :setup_scenario, :setup => :thing1
144
+
145
+ it "sets up" do
146
+ thing1.should == "set up"
147
+ end
148
+ end
149
+
150
+ describe "calling setup via block" do
151
+ scenario :setup_scenario do
152
+ setup_thing2( "a", "b" )
153
+ end
154
+
155
+ it "sets up" do
156
+ thing2.should == ["a", "b"]
157
+ end
158
+ end
159
+
160
+ describe "calling setup via opts hash and block" do
161
+ scenario :setup_scenario, :setup => [:thing1] do
162
+ setup_thing2( "a", "b" )
163
+ end
164
+
165
+ it "works" do
166
+ thing1.should == "set up"
167
+ thing2.should == ["a", "b"]
168
+ end
56
169
  end
57
170
  end
@@ -1,33 +1,65 @@
1
1
  describe_scenario :simple do
2
+ before :all do
3
+ @scenario_all = true
4
+ end
5
+
2
6
  before :each do
3
- @zomg_each = true
7
+ @scenario_each = true
4
8
  end
5
9
 
6
- before :all do
7
- @zomg_all = true
10
+ let :let_block do
11
+ @let_block ||= 0
12
+ @let_block += 1
8
13
  end
9
14
 
10
- def lol
11
- "lol"
15
+ def scenario_method
16
+ "present"
12
17
  end
13
18
  end
14
19
 
15
20
  describe_scenario :scenario_one do
21
+ before :each do
22
+ @scenario_one_present = true
23
+ end
24
+
16
25
  def one
17
26
  "present"
18
27
  end
19
28
 
20
- def zero_cool?
29
+ def scenario_method?
21
30
  false
22
31
  end
23
32
  end
24
33
 
25
34
  describe_scenario :scenario_two do
35
+ before :each do
36
+ @scenario_two_present = true
37
+ end
38
+
26
39
  def two
27
40
  "also present"
28
41
  end
29
42
 
30
- def zero_cool?
43
+ def scenario_method?
31
44
  true
32
45
  end
33
46
  end
47
+
48
+ describe_scenario :setup_scenario do
49
+
50
+ setup_for :thing1 do
51
+ @thing1 = "set up"
52
+ end
53
+
54
+ setup_for :thing2 do |*params|
55
+ @thing2 = params
56
+ end
57
+
58
+ def thing1
59
+ @thing1
60
+ end
61
+
62
+ def thing2
63
+ @thing2
64
+ end
65
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scenario
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 0
10
- version: 0.3.0
9
+ - 1
10
+ version: 0.3.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tyson Tate
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-21 00:00:00 -07:00
18
+ date: 2011-05-23 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency