fixturama 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/README.md +14 -1
- data/fixturama.gemspec +1 -1
- data/lib/fixturama/stubs/{actions → chain/actions}/raise.rb +1 -1
- data/lib/fixturama/stubs/{actions → chain/actions}/return.rb +1 -1
- data/lib/fixturama/stubs/{actions.rb → chain/actions.rb} +1 -1
- data/lib/fixturama/stubs/{arguments.rb → chain/arguments.rb} +2 -2
- data/lib/fixturama/stubs/chain.rb +32 -24
- data/lib/fixturama/stubs/const.rb +42 -0
- data/lib/fixturama/stubs.rb +33 -23
- data/spec/fixturama/stub_fixture/_spec.rb +12 -1
- data/spec/fixturama/stub_fixture/stub.yml +3 -0
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48e09d4c2f2bc2eade2e53915d3a5e6ea365b7af3774da48f72bf9ca30fd1554
|
4
|
+
data.tar.gz: e451162201f818ca015827b3eb866ac87515730686cffb739fe8ca061e39800e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52394f8c73eb43946a084e830b0364106b3ce5dedcc9e7155bef17b25b0a7a6df1ccbb55c2252ff64565a64d299b5db7df2c09a2c6bfa2f649c422031126b89a
|
7
|
+
data.tar.gz: 273c09ddb0ef0f0b3270693c2d3eb38992ae91cd72821e1b0059198b840c8b91946aefe3ae32ca8413deeaaca5691f830fa5a5d9fff64fbd44ab1d8c4c2782aa
|
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
6
6
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
7
7
|
|
8
|
+
## [0.0.5] - [2018-06-04]
|
9
|
+
|
10
|
+
### Added
|
11
|
+
|
12
|
+
- Support for stubbing constants (nepalez)
|
13
|
+
|
14
|
+
```yaml
|
15
|
+
# Stub constant TIMEOUT_SEC to 10
|
16
|
+
---
|
17
|
+
- const: TIMEOUT_SEC
|
18
|
+
value: 10
|
19
|
+
```
|
20
|
+
|
8
21
|
## [0.0.4] - [2018-05-22]
|
9
22
|
|
10
23
|
### Added
|
@@ -68,3 +81,4 @@ This is a first public release with features extracted from production app.
|
|
68
81
|
[0.0.2]: https://github.com/nepalez/fixturama/compare/v0.0.1...v0.0.2
|
69
82
|
[0.0.3]: https://github.com/nepalez/fixturama/compare/v0.0.2...v0.0.3
|
70
83
|
[0.0.4]: https://github.com/nepalez/fixturama/compare/v0.0.3...v0.0.4
|
84
|
+
[0.0.5]: https://github.com/nepalez/fixturama/compare/v0.0.4...v0.0.5
|
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
Collection of helpers for dealing with fixtures in [RSpec][rspec]
|
4
4
|
|
5
|
+
Read the [post about the library on dev.to][dev_to].
|
6
|
+
|
5
7
|
<a href="https://evilmartians.com/">
|
6
8
|
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"></a>
|
7
9
|
|
@@ -90,13 +92,20 @@ The seed (`seed_fixture`) file should be a YAML/JSON with opinionated parameters
|
|
90
92
|
|
91
93
|
Use the `count: 2` key to create more objects at once.
|
92
94
|
|
93
|
-
Another opinionated format we use for stubs (`stub_fixture`)
|
95
|
+
Another opinionated format we use for stubs (`stub_fixture`). The gem supports stubbing both message chains and constants.
|
96
|
+
|
97
|
+
For message chains:
|
94
98
|
|
95
99
|
- `class` for stubbed class
|
96
100
|
- `chain` for messages chain
|
97
101
|
- `arguments` (optional) for specific arguments
|
98
102
|
- `actions` for an array of actions for consecutive invocations of the chain
|
99
103
|
|
104
|
+
For constants:
|
105
|
+
|
106
|
+
- `const` for stubbed constant
|
107
|
+
- `value` for a value of the constant
|
108
|
+
|
100
109
|
Every action either `return` some value, or `raise` some exception
|
101
110
|
|
102
111
|
```yaml
|
@@ -126,6 +135,9 @@ Every action either `return` some value, or `raise` some exception
|
|
126
135
|
actions:
|
127
136
|
- return: true
|
128
137
|
- raise: ActiveRecord::RecordNotFound
|
138
|
+
|
139
|
+
- const: NOTIFIER_TIMEOUT_SEC
|
140
|
+
value: 10
|
129
141
|
```
|
130
142
|
|
131
143
|
```graphql
|
@@ -168,3 +180,4 @@ The gem is available as open source under the terms of the [MIT License][license
|
|
168
180
|
[license]: http://opensource.org/licenses/MIT
|
169
181
|
[factory-bot]: https://github.com/thoughtbot/factory_bot
|
170
182
|
[rspec]: https://rspec.info/
|
183
|
+
[dev_to]: https://dev.to/evilmartians/a-fixture-based-approach-to-interface-testing-in-rails-2cd4
|
data/fixturama.gemspec
CHANGED
@@ -2,7 +2,7 @@ module Fixturama
|
|
2
2
|
#
|
3
3
|
# Collection of arguments for a stub with a list of actions to be called
|
4
4
|
#
|
5
|
-
class Stubs::Arguments
|
5
|
+
class Stubs::Chain::Arguments
|
6
6
|
attr_reader :chain, :arguments
|
7
7
|
|
8
8
|
#
|
@@ -15,7 +15,7 @@ module Fixturama
|
|
15
15
|
actions.each do |settings|
|
16
16
|
settings = Utils.symbolize_hash(settings)
|
17
17
|
repeat = [0, settings.fetch(:repeat, 1).to_i].max
|
18
|
-
repeat.times { list << Stubs::Actions.build(self, settings) }
|
18
|
+
repeat.times { list << Stubs::Chain::Actions.build(self, settings) }
|
19
19
|
end
|
20
20
|
|
21
21
|
self
|
@@ -3,8 +3,20 @@ module Fixturama
|
|
3
3
|
# Stubbed chain of messages
|
4
4
|
#
|
5
5
|
class Stubs::Chain
|
6
|
+
require_relative "chain/actions"
|
7
|
+
require_relative "chain/arguments"
|
8
|
+
|
6
9
|
attr_reader :receiver, :messages
|
7
10
|
|
11
|
+
#
|
12
|
+
# Human-readable representation of the chain
|
13
|
+
# @return [String]
|
14
|
+
#
|
15
|
+
def to_s
|
16
|
+
"#{receiver}.#{messages.join(".")}"
|
17
|
+
end
|
18
|
+
alias to_str to_s
|
19
|
+
|
8
20
|
#
|
9
21
|
# Register new action for some arguments
|
10
22
|
#
|
@@ -12,11 +24,11 @@ module Fixturama
|
|
12
24
|
# @option (see Fixturama::Stubs::Arguments#add_action)
|
13
25
|
# @return [self]
|
14
26
|
#
|
15
|
-
def
|
27
|
+
def update!(actions:, arguments: nil, **)
|
16
28
|
Utils.array(arguments).tap do |args|
|
17
29
|
stub = find_by(args)
|
18
30
|
unless stub
|
19
|
-
stub = Stubs::Arguments.new(self, args)
|
31
|
+
stub = Stubs::Chain::Arguments.new(self, args)
|
20
32
|
stubs << stub
|
21
33
|
end
|
22
34
|
stub.add!(*actions)
|
@@ -27,31 +39,16 @@ module Fixturama
|
|
27
39
|
end
|
28
40
|
|
29
41
|
#
|
30
|
-
#
|
31
|
-
# @return [self] itself
|
42
|
+
# Apply the stub to RSpec example
|
32
43
|
#
|
33
|
-
def
|
34
|
-
|
35
|
-
end
|
44
|
+
def apply!(example)
|
45
|
+
reset!
|
36
46
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
# @raise [StandardError]
|
41
|
-
#
|
42
|
-
def call!(actual_arguments)
|
43
|
-
stub = stubs.find { |item| item.applicable_to?(actual_arguments) }
|
44
|
-
raise "Unexpected arguments #{actual_arguments}" unless stub
|
45
|
-
|
46
|
-
stub.call_next!
|
47
|
-
end
|
47
|
+
call_action = example.send(:receive_message_chain, *messages) do |*args|
|
48
|
+
call! args
|
49
|
+
end
|
48
50
|
|
49
|
-
|
50
|
-
# Human-readable representation of the chain
|
51
|
-
# @return [String]
|
52
|
-
#
|
53
|
-
def to_s
|
54
|
-
"#{receiver}.#{messages.join(".")}"
|
51
|
+
example.send(:allow, receiver).to call_action
|
55
52
|
end
|
56
53
|
|
57
54
|
private
|
@@ -74,5 +71,16 @@ module Fixturama
|
|
74
71
|
def find_by(arguments)
|
75
72
|
stubs.find { |stub| stub.arguments == arguments }
|
76
73
|
end
|
74
|
+
|
75
|
+
def reset!
|
76
|
+
tap { stubs.each(&:reset!) }
|
77
|
+
end
|
78
|
+
|
79
|
+
def call!(actual_arguments)
|
80
|
+
stub = stubs.find { |item| item.applicable_to?(actual_arguments) }
|
81
|
+
raise "Unexpected arguments #{actual_arguments}" unless stub
|
82
|
+
|
83
|
+
stub.call_next!
|
84
|
+
end
|
77
85
|
end
|
78
86
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Fixturama
|
2
|
+
#
|
3
|
+
# Definition for stubbing a constant
|
4
|
+
#
|
5
|
+
class Stubs::Const
|
6
|
+
attr_reader :const, :value
|
7
|
+
|
8
|
+
#
|
9
|
+
# Human-readable representation of the chain
|
10
|
+
# @return [String]
|
11
|
+
#
|
12
|
+
def to_s
|
13
|
+
const.to_s
|
14
|
+
end
|
15
|
+
alias to_str to_s
|
16
|
+
|
17
|
+
#
|
18
|
+
# Overload the definition for the constant
|
19
|
+
# @option [Object] value
|
20
|
+
# @return [self]
|
21
|
+
#
|
22
|
+
def update!(value:, **)
|
23
|
+
@value = value
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
#
|
28
|
+
# Apply the stub to RSpec example
|
29
|
+
# @return [self]
|
30
|
+
#
|
31
|
+
def apply!(example)
|
32
|
+
example.send(:stub_const, const, value)
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def initialize(const:, **)
|
39
|
+
@const = const.to_s
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/fixturama/stubs.rb
CHANGED
@@ -3,47 +3,57 @@ module Fixturama
|
|
3
3
|
# Collection of stubbed calls
|
4
4
|
#
|
5
5
|
class Stubs
|
6
|
-
require_relative "stubs/actions"
|
7
|
-
require_relative "stubs/arguments"
|
8
6
|
require_relative "stubs/chain"
|
7
|
+
require_relative "stubs/const"
|
9
8
|
|
10
9
|
#
|
11
10
|
# Register new action and apply the corresponding stub
|
12
11
|
#
|
13
|
-
# @
|
14
|
-
# @option [Array<#to_s>] :chain Methods chain for stubbing
|
15
|
-
# @option (see Fixturama::Stubs::Method#add)
|
12
|
+
# @params [Hash<#to_s, _>] options
|
16
13
|
# @return [self] itself
|
17
14
|
#
|
18
15
|
def add(options)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
options.select { |key| %i[class chain].include?(key) }.tap do |anchors|
|
23
|
-
chains[anchors] ||= Chain.new(anchors)
|
24
|
-
chains[anchors].add(options)
|
25
|
-
end
|
26
|
-
end
|
16
|
+
options = symbolize(options)
|
17
|
+
find_or_create_stub!(options)&.update!(options)
|
18
|
+
self
|
27
19
|
end
|
28
20
|
|
29
21
|
#
|
30
22
|
# Applies the stub to RSpec example
|
31
23
|
#
|
32
24
|
def apply(example)
|
33
|
-
|
34
|
-
chain.reset!
|
35
|
-
call_action = \
|
36
|
-
example.send(:receive_message_chain, *chain.messages) do |*args|
|
37
|
-
chain.call! args
|
38
|
-
end
|
39
|
-
example.send(:allow, chain.receiver).to call_action
|
40
|
-
end
|
25
|
+
@stubs.values.each { |stub| stub.apply!(example) }
|
41
26
|
end
|
42
27
|
|
43
28
|
private
|
44
29
|
|
45
|
-
def
|
46
|
-
@
|
30
|
+
def initialize
|
31
|
+
@stubs = {}
|
32
|
+
end
|
33
|
+
|
34
|
+
def find_or_create_stub!(options)
|
35
|
+
case stub_type(options)
|
36
|
+
when :message_chain
|
37
|
+
anchor = options.slice(:class, :chain)
|
38
|
+
@stubs[anchor] ||= Chain.new(anchor)
|
39
|
+
when :constant
|
40
|
+
anchor = options.slice(:const)
|
41
|
+
@stubs[anchor] ||= Const.new(anchor)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def stub_type(options)
|
46
|
+
return :message_chain if options[:class]
|
47
|
+
return :constant if options[:const]
|
48
|
+
|
49
|
+
raise ArgumentError, <<~MESSAGE
|
50
|
+
Cannot figure out what to stub from #{options}.
|
51
|
+
You should define either a class and a message chain, or some const.
|
52
|
+
MESSAGE
|
53
|
+
end
|
54
|
+
|
55
|
+
def symbolize(options)
|
56
|
+
Hash(options).transform_keys { |key| key.to_s.to_sym }
|
47
57
|
end
|
48
58
|
end
|
49
59
|
end
|
@@ -15,7 +15,7 @@ RSpec.describe "stub_fixture" do
|
|
15
15
|
it { is_expected.to eq [5] }
|
16
16
|
end
|
17
17
|
|
18
|
-
context "
|
18
|
+
context "when message chain stubbed" do
|
19
19
|
before { stub_fixture "#{__dir__}/stub.yml" }
|
20
20
|
|
21
21
|
context "with a :raise option" do
|
@@ -66,4 +66,15 @@ RSpec.describe "stub_fixture" do
|
|
66
66
|
end
|
67
67
|
end
|
68
68
|
end
|
69
|
+
|
70
|
+
context "when constant stubbed" do
|
71
|
+
before do
|
72
|
+
TIMEOUT = 20
|
73
|
+
stub_fixture "#{__dir__}/stub.yml"
|
74
|
+
end
|
75
|
+
|
76
|
+
it "stubs the constant" do
|
77
|
+
expect(TIMEOUT).to eq 10
|
78
|
+
end
|
79
|
+
end
|
69
80
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fixturama
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kozin (nepalez)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: factory_bot
|
@@ -117,11 +117,12 @@ files:
|
|
117
117
|
- lib/fixturama/rspec.rb
|
118
118
|
- lib/fixturama/seed.rb
|
119
119
|
- lib/fixturama/stubs.rb
|
120
|
-
- lib/fixturama/stubs/actions.rb
|
121
|
-
- lib/fixturama/stubs/actions/raise.rb
|
122
|
-
- lib/fixturama/stubs/actions/return.rb
|
123
|
-
- lib/fixturama/stubs/arguments.rb
|
124
120
|
- lib/fixturama/stubs/chain.rb
|
121
|
+
- lib/fixturama/stubs/chain/actions.rb
|
122
|
+
- lib/fixturama/stubs/chain/actions/raise.rb
|
123
|
+
- lib/fixturama/stubs/chain/actions/return.rb
|
124
|
+
- lib/fixturama/stubs/chain/arguments.rb
|
125
|
+
- lib/fixturama/stubs/const.rb
|
125
126
|
- lib/fixturama/utils.rb
|
126
127
|
- spec/fixturama/load_fixture/_spec.rb
|
127
128
|
- spec/fixturama/load_fixture/data.json
|