rspec-example_steps 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
+
5
+ gem 'rspec', '2.7.0'
data/README.md CHANGED
@@ -32,7 +32,85 @@ Given/When/Then steps for RSpec examples
32
32
  When I search something
33
33
  Then I should see result
34
34
 
35
- ## References
35
+
36
+ ### Shared steps
37
+
38
+ Use _shared_steps_ do define block that will be evaluated in the context of example using _include_steps_.
39
+
40
+ Shared _steps_ behavior is simular to shared _example_ but context is example nor example_group.
41
+
42
+ ### Example with shared steps
43
+
44
+ shared_steps "login" do
45
+ When "I go to login page" do
46
+ page.visit '/login'
47
+ end
48
+ When "I put credentials" do
49
+ page.fill_in 'Login', :with => 'jack@example.com'
50
+ page.fill_in 'Password', :with => 'password''
51
+ end
52
+ Then "I should be logged in" do
53
+ page.status_code.should == 200
54
+ page.should have_content("Welcome jack@example.com")
55
+ end
56
+ end
57
+
58
+ shared_steps "logout" do
59
+ page.visit '/logout'
60
+ page.status_code.should == 200
61
+ end
62
+
63
+ context "user flow"
64
+ Steps "User updates profile description" do
65
+ include_steps "login"
66
+ When "I update profile description" do
67
+ ...
68
+ end
69
+ include_steps "logout"
70
+ end
71
+
72
+ Steps "User updates profile avatar" do
73
+ include_steps "login"
74
+ When "I update profile avatar" do
75
+ ...
76
+ end
77
+ include_steps "logout"
78
+ end
79
+ end
80
+
81
+ ### Passing arguments to shared steps
82
+
83
+ It's possible to customize shared steps. See example
84
+
85
+ ### Example with shared steps with arguments
86
+
87
+ shared_steps "login" do |email, password|
88
+ When "I go to login page" do
89
+ page.visit '/login'
90
+ end
91
+ When "I put credentials" do
92
+ page.fill_in 'Login', :with => email
93
+ page.fill_in 'Password', :with => password
94
+ end
95
+ end
96
+
97
+ shared_steps "invalid login" do
98
+ Then "I should see login error" do
99
+ ...
100
+ end
101
+ end
102
+
103
+ Steps "User provides wrong email" do
104
+ include_steps "login", 'jack', 'qwerty'
105
+ include_steps "invalid login"
106
+ end
107
+
108
+ Steps "User provides wrong password" do
109
+ include_steps "login", 'jack@example.com', 'bla'
110
+ include_steps "invalid login"
111
+ end
112
+
113
+ ## Alternatives
36
114
 
37
115
  * [rspec-steps](https://github.com/LRDesign/rspec-steps)
38
116
  * [rspec-given](https://github.com/jimweirich/rspec-given)
@@ -1,22 +1,29 @@
1
1
  module RSpec
2
2
  module ExampleSteps
3
3
  module ExampleGroup
4
+ def include_steps(*args)
5
+ name = args.shift
6
+ shared_block = RSpec.world.shared_example_steps[name]
7
+ shared_block or raise ArgumentError, "Could not find shared steps #{name.inspect}"
8
+ instance_eval_with_args(*args, &shared_block)
9
+ end
10
+
4
11
  def Given(message)
5
- ::RSpec.configuration.reporter.example_step_started(self, :given, message)
12
+ RSpec.world.reporter.example_step_started(self, :given, message)
6
13
  yield
7
- ::RSpec.configuration.reporter.example_step_passed(self, :given, message)
14
+ RSpec.world.reporter.example_step_passed(self, :given, message)
8
15
  end
9
16
 
10
17
  def When(message)
11
- ::RSpec.configuration.reporter.example_step_started(self, :when, message)
18
+ RSpec.world.reporter.example_step_started(self, :when, message)
12
19
  yield
13
- ::RSpec.configuration.reporter.example_step_passed(self, :when, message)
20
+ RSpec.world.reporter.example_step_passed(self, :when, message)
14
21
  end
15
22
 
16
23
  def Then(message)
17
- ::RSpec.configuration.reporter.example_step_started(self, :then, message)
24
+ RSpec.world.reporter.example_step_started(self, :then, message)
18
25
  yield
19
- ::RSpec.configuration.reporter.example_step_passed(self, :then, message)
26
+ RSpec.world.reporter.example_step_passed(self, :then, message)
20
27
  end
21
28
  end
22
29
  end
@@ -0,0 +1,20 @@
1
+ module RSpec
2
+ module ExampleSteps
3
+ module SharedSteps
4
+
5
+ def shared_steps(name, &block)
6
+ ensure_shared_example_steps_name_not_taken(name)
7
+ RSpec.world.shared_example_steps[name] = block
8
+ end
9
+
10
+ private
11
+
12
+ def ensure_shared_example_steps_name_not_taken(name)
13
+ if RSpec.world.shared_example_steps.has_key?(name)
14
+ raise ArgumentError.new("Shared example steps '#{name}' already exists")
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ module RSpec
2
+ module ExampleSteps
3
+ module World
4
+ def shared_example_steps
5
+ @shared_example_steps ||= {}
6
+ end
7
+ end
8
+ end
9
+ end
@@ -2,15 +2,21 @@ require 'rspec/core/formatters/base_formatter'
2
2
  require 'rspec/core/formatters/documentation_formatter'
3
3
  require "rspec/core/example_group"
4
4
  require 'rspec/core/reporter'
5
+ require 'rspec/core/world'
5
6
 
6
7
  require 'rspec/example_steps/base_formatter'
7
8
  require 'rspec/example_steps/documentation_formatter'
8
9
  require "rspec/example_steps/example_group"
9
10
  require 'rspec/example_steps/reporter'
11
+ require 'rspec/example_steps/world'
10
12
 
11
13
  RSpec::Core::Formatters::BaseFormatter.send :include, RSpec::ExampleSteps::BaseFormatter
12
14
  RSpec::Core::Formatters::DocumentationFormatter.send :include, RSpec::ExampleSteps::DocumentationFormatter
13
15
  RSpec::Core::ExampleGroup.send :include, RSpec::ExampleSteps::ExampleGroup
14
16
  RSpec::Core::Reporter.send :include, RSpec::ExampleSteps::Reporter
17
+ RSpec::Core::World.send :include, RSpec::ExampleSteps::World
15
18
 
16
19
  RSpec::Core::ExampleGroup.define_example_method :Steps, :with_steps => true
20
+
21
+ require 'rspec/example_steps/shared_steps'
22
+ include RSpec::ExampleSteps::SharedSteps
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "rspec-example_steps"
6
- s.version = "0.0.2"
6
+ s.version = "0.1.0"
7
7
  s.authors = ["Andriy Yanko"]
8
8
  s.email = ["andriy.yanko@gmail.com"]
9
9
  s.homepage = "https://github.com/railsware/rspec-example_steps"
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe "example steps" do
4
+
5
+ describe "Given/When/Then" do
6
+
7
+ Steps "should execute blocks" do
8
+ Given "thing" do
9
+ @thing = "Given value"
10
+ end
11
+
12
+ When "action" do
13
+ @action = "When value"
14
+ end
15
+
16
+ Then "result" do
17
+ @result = "Then value"
18
+ end
19
+
20
+ @thing.should == "Given value"
21
+ @action.should == "When value"
22
+ @result.should == "Then value"
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ shared_steps "push steps" do
4
+ Given "empty pipe" do
5
+ pipe.clear
6
+ end
7
+
8
+ When "push data" do
9
+ pipe.push "hello"
10
+ end
11
+
12
+ Then "pipe should have data" do
13
+ pipe.items.should == ["hello"]
14
+ end
15
+ end
16
+
17
+ shared_steps "push with arguments steps" do |value|
18
+ When "push data" do
19
+ pipe.push value
20
+ end
21
+ end
22
+
23
+ shared_steps "pull steps" do
24
+ When "pull data" do
25
+ pipe.pull
26
+ end
27
+
28
+ Then "pipe should be empty" do
29
+ pipe.items.should == []
30
+ end
31
+ end
32
+
33
+ describe "shared steps" do
34
+
35
+ let(:pipe) do
36
+ Class.new do
37
+ def initialize
38
+ @values = []
39
+ end
40
+
41
+ def items
42
+ @values
43
+ end
44
+
45
+ def clear
46
+ @values = []
47
+ end
48
+
49
+ def push(value)
50
+ @values << value
51
+ end
52
+
53
+ def pull
54
+ @values.shift
55
+ end
56
+
57
+ end.new
58
+ end
59
+
60
+ Steps "push and pull steps" do
61
+ include_steps "push steps"
62
+ include_steps "pull steps"
63
+ end
64
+
65
+ Steps "push, count and pull" do
66
+ include_steps "push steps"
67
+ Then "pipe should have 1 item" do
68
+ pipe.items.size.should == 1
69
+ end
70
+ include_steps "pull steps"
71
+ end
72
+
73
+ Steps "push with arguments and pull" do
74
+ include_steps "push with arguments steps", "hi"
75
+ pipe.items.should == ["hi"]
76
+ include_steps "pull steps"
77
+ end
78
+ end
@@ -0,0 +1 @@
1
+ require 'rspec/example_steps'
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 2
10
- version: 0.0.2
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andriy Yanko
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-12-29 00:00:00 +02:00
18
+ date: 2011-12-30 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -53,7 +53,12 @@ files:
53
53
  - lib/rspec/example_steps/documentation_formatter.rb
54
54
  - lib/rspec/example_steps/example_group.rb
55
55
  - lib/rspec/example_steps/reporter.rb
56
+ - lib/rspec/example_steps/shared_steps.rb
57
+ - lib/rspec/example_steps/world.rb
56
58
  - rspec-example_steps.gemspec
59
+ - spec/example_steps_spec.rb
60
+ - spec/shared_steps_spec.rb
61
+ - spec/spec_helper.rb
57
62
  has_rdoc: true
58
63
  homepage: https://github.com/railsware/rspec-example_steps
59
64
  licenses: []
@@ -88,5 +93,7 @@ rubygems_version: 1.5.2
88
93
  signing_key:
89
94
  specification_version: 3
90
95
  summary: Given/When/Then steps for RSpec examples
91
- test_files: []
92
-
96
+ test_files:
97
+ - spec/example_steps_spec.rb
98
+ - spec/shared_steps_spec.rb
99
+ - spec/spec_helper.rb