rspec-puppet-utils 2.0.6 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +17 -10
- data/Rakefile +2 -2
- data/lib/mock_function.rb +19 -8
- data/rspec-puppet-utils.gemspec +1 -1
- data/spec/classes/mock_function_spec.rb +69 -22
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
SHA512:
|
3
|
-
metadata.gz: 106fa2607e4f29ce28dee277ad33e57af9172a0950ff6d7e1603aced6853511a71dc826e56e9224865d8671437a1e531b6d64fc89f5551b5e7224a062988d88c
|
4
|
-
data.tar.gz: faa7027e9499454bf5ae605921f64356485385025b5da3b290bf2bb37cd48435237d42cbf8d185f99d56d5a17e2c9ad6d754c7e242b829e31dc52a2b9e9a9b61
|
5
2
|
SHA1:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9e1b2ce2d09e5a50fff68ae9cc3f1e7e639ec18
|
4
|
+
data.tar.gz: c8184a93084596ce109c344c438107a4914f4cfc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e51385d1bdf52f5ade7292fc13cf3b58f27008db4db08ff9c4fb056af3d091042054b3b75737530f26bfbe7ed27d3c98149fa84137823ddb9635ae0ad01c4a77
|
7
|
+
data.tar.gz: 7aa2fe95f16d78ea0382451c6b9de02c3fb2286193f445d74a4c7df308da7d0d7bca6dfd935d334ecadbd8a2448202a0ad024dc9b80974aa3a82bfcf4250b712
|
data/README.md
CHANGED
@@ -8,6 +8,14 @@ See [release notes](../../wiki/Release-Notes) about latest version
|
|
8
8
|
|
9
9
|
### MockFunction
|
10
10
|
|
11
|
+
#####Update:
|
12
|
+
|
13
|
+
As of version 2.1.0 the `#stub` and `#expect` methods have been superseded by `#stubbed` and `#expected` so that you can use parameter matchers. The only difference in usage from previous versions is that the methods take a set of parameters rather than a single array (e.g. `f.expected.with(1, 2, 3)` instead of `f.expect.with([1, 2, 3])`)
|
14
|
+
|
15
|
+
The change is backwards compatible so `#stub` and `#expect` are still available and function as before
|
16
|
+
|
17
|
+
#####Usage:
|
18
|
+
|
11
19
|
The basic usage is to create your mock function with `MockFunction.new` and then use `mocha` to stub any particular calls that you need
|
12
20
|
|
13
21
|
```ruby
|
@@ -16,14 +24,13 @@ require 'spec_helper'
|
|
16
24
|
describe 'foo::bar' do
|
17
25
|
|
18
26
|
let!(:add_stuff) { MockFunction.new('add_stuff') { |f|
|
19
|
-
f.
|
27
|
+
f.stubbed.with(1, 2).returns(3)
|
20
28
|
}
|
21
29
|
}
|
22
30
|
|
23
31
|
it 'should do something with add_stuff' do
|
24
32
|
# Specific stub for this test
|
25
|
-
add_stuff.
|
26
|
-
...
|
33
|
+
add_stuff.stubbed.with(2, 3).returns(5)
|
27
34
|
...
|
28
35
|
end
|
29
36
|
end
|
@@ -37,8 +44,8 @@ MockFunction.new('func', {:type => :statement})
|
|
37
44
|
You can mock Hiera:
|
38
45
|
```ruby
|
39
46
|
MockFunction.new('hiera') { |f|
|
40
|
-
f.
|
41
|
-
f.
|
47
|
+
f.stubbed.with('non-ex').raises(Puppet::ParseError.new('Key not found'))
|
48
|
+
f.stubbed.with('db-password').returns('password1')
|
42
49
|
}
|
43
50
|
```
|
44
51
|
You handle when the functions are created yourself, e.g. you can assign it to a local variable `func = MockFunction...` create it in a before block `before(:each) do MockFunction... end` or use let `let!(:func) { MockFunction... }`
|
@@ -48,15 +55,15 @@ If you use let, **use `let!()` and not `let()`**, this is because lets are lazy-
|
|
48
55
|
Also if you use `let` when mocking hiera, **you can't use `:hiera` as the name due to conflicts** so you have to do something like `let!(:mock_hiera) { MockFunction.new('hiera') }`
|
49
56
|
|
50
57
|
#####Mocha stubs and expects:
|
51
|
-
`f.
|
58
|
+
`f.stubbed` and `f.expected` are helper methods for `f.stubs(:execute)` and `f.expects(:execute)`
|
52
59
|
|
53
|
-
Internally `#
|
60
|
+
Internally `#expected` will clear the rspec-puppet catalog cache. This is because rspec-puppet will only re-compile the catalog for a test if `:title`, `:params`, or `:facts` are changed. This means that if you setup an expectaion in a test, it might not be satisfied because the catalog was already compiled for a previous test, and so the functions weren't called!
|
54
61
|
|
55
|
-
Clearing the cache ensures tests aren't coupled and order dependent. The downside is that the catalog isn't cached and has to be re-compiled which slows down your tests. If you're concerned about performance and you are explicitly changing `:title`, `:params`, or `:facts` for a test, you can keep the cache intact with `f.
|
62
|
+
Clearing the cache ensures tests aren't coupled and order dependent. The downside is that the catalog isn't cached and has to be re-compiled which slows down your tests. If you're concerned about performance and you are explicitly changing `:title`, `:params`, or `:facts` for a test, you can keep the cache intact with `f.expected(:keep_cache)`
|
56
63
|
|
57
64
|
#####Notes:
|
58
|
-
- You always stub the `
|
59
|
-
- The `
|
65
|
+
- You always stub the `execute` method as that gets called internally
|
66
|
+
- The `execute` method takes a set of arguments instead of an array of arguments
|
60
67
|
|
61
68
|
### TemplateHarness
|
62
69
|
|
data/Rakefile
CHANGED
data/lib/mock_function.rb
CHANGED
@@ -16,22 +16,33 @@ module RSpecPuppetUtils
|
|
16
16
|
def initialize(name, options = {})
|
17
17
|
parse_options! options
|
18
18
|
this = self
|
19
|
-
Puppet::Parser::Functions.newfunction(name.to_sym, options) { |args| this.call args}
|
19
|
+
Puppet::Parser::Functions.newfunction(name.to_sym, options) { |args| this.call args }
|
20
20
|
yield self if block_given?
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
def call(args)
|
24
|
+
execute *args
|
25
|
+
end
|
26
|
+
|
27
|
+
def execute(*args)
|
28
|
+
args
|
29
|
+
end
|
30
|
+
|
31
|
+
def stubbed
|
32
|
+
self.stubs(:execute)
|
33
|
+
end
|
34
|
+
|
35
|
+
def expected(*args)
|
36
|
+
RSpec::Puppet::Support.clear_cache unless args.include? :keep_cache
|
37
|
+
self.expects(:execute)
|
29
38
|
end
|
30
39
|
|
40
|
+
# Use stubbed instead, see readme
|
31
41
|
def stub
|
32
42
|
self.stubs(:call)
|
33
43
|
end
|
34
44
|
|
45
|
+
# Use expected instead, see readme
|
35
46
|
def expect(*args)
|
36
47
|
RSpec::Puppet::Support.clear_cache unless args.include? :keep_cache
|
37
48
|
self.expects(:call)
|
data/rspec-puppet-utils.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
Gem::Specification.new do |gem|
|
3
3
|
gem.name = 'rspec-puppet-utils'
|
4
|
-
gem.version = '2.0
|
4
|
+
gem.version = '2.1.0'
|
5
5
|
gem.description = 'Helper classes for mock/stub functions, templates and hierdata'
|
6
6
|
gem.summary = ''
|
7
7
|
gem.author = 'Tom Poulton'
|
@@ -14,39 +14,39 @@ describe MockFunction do
|
|
14
14
|
func_name = 'my_func'
|
15
15
|
func_sym = func_name.to_sym
|
16
16
|
|
17
|
-
it '
|
17
|
+
it 'adds new function to puppet' do
|
18
18
|
name = 'mock_func'
|
19
19
|
func = MockFunction.new name
|
20
20
|
expect(Puppet::Parser::Functions.function(name.to_sym)).to eq "function_#{name}"
|
21
21
|
end
|
22
22
|
|
23
|
-
it '
|
23
|
+
it 'defaults to :rvalue type' do
|
24
24
|
func = MockFunction.new func_name
|
25
25
|
expect(Puppet::Parser::Functions.rvalue?(func_sym)).to eq true
|
26
26
|
end
|
27
27
|
|
28
|
-
it '
|
28
|
+
it 'defaults to :rvalue type if missing from options' do
|
29
29
|
func = MockFunction.new func_name, {}
|
30
30
|
expect(Puppet::Parser::Functions.rvalue?(func_sym)).to eq true
|
31
31
|
end
|
32
32
|
|
33
|
-
it '
|
33
|
+
it 'allows type to be set' do
|
34
34
|
func = MockFunction.new func_name, {:type => :statement}
|
35
35
|
expect(Puppet::Parser::Functions.rvalue?(func_sym)).to eq false
|
36
36
|
end
|
37
37
|
|
38
|
-
it '
|
38
|
+
it 'only allows :rvalue or :statement for type' do
|
39
39
|
expect {
|
40
40
|
MockFunction.new func_name, {:type => :error}
|
41
41
|
}.to raise_error ArgumentError, 'Type should be :rvalue or :statement, not error'
|
42
42
|
end
|
43
43
|
|
44
|
-
it '
|
44
|
+
it 'allows arity to be set' do
|
45
45
|
func = MockFunction.new func_name, {:arity => 3}
|
46
46
|
expect(Puppet::Parser::Functions.arity(func_sym)).to eq 3
|
47
47
|
end
|
48
48
|
|
49
|
-
it '
|
49
|
+
it 'only allows arity to be an integer' do
|
50
50
|
expect {
|
51
51
|
MockFunction.new func_name, {:arity => 'oops'}
|
52
52
|
}.to raise_error ArgumentError, 'arity should be an integer'
|
@@ -58,33 +58,80 @@ describe MockFunction do
|
|
58
58
|
|
59
59
|
let(:func) { MockFunction.new('func') }
|
60
60
|
|
61
|
-
it '
|
62
|
-
expect(func.respond_to?(:call)).to eq false
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'should be stubable' do
|
61
|
+
it 'is stubable' do
|
66
62
|
func.stubs(:call)
|
67
63
|
expect(func.respond_to?(:call)).to eq true
|
68
64
|
end
|
69
65
|
|
70
|
-
it '
|
66
|
+
it 'is called by puppet function' do
|
71
67
|
func.stubs(:call).returns('penguin')
|
72
68
|
result = scope.function_func []
|
73
69
|
expect(result).to eq 'penguin'
|
74
70
|
end
|
75
71
|
|
76
|
-
it '
|
72
|
+
it 'is passed puppet function args' do
|
77
73
|
func.expects(:call).with([1, 2, 3]).once
|
78
74
|
scope.function_func [1, 2, 3]
|
79
75
|
end
|
80
76
|
|
77
|
+
it 'passes function args to execute method' do
|
78
|
+
func.expects(:execute).with(1, 2, 3)
|
79
|
+
func.call [1, 2, 3]
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
describe '#stubbed' do
|
85
|
+
|
86
|
+
let(:func) { MockFunction.new('func') }
|
87
|
+
|
88
|
+
it 'stubs #execute' do
|
89
|
+
expectation = func.stubbed
|
90
|
+
expect(expectation).to be_a Mocha::Expectation
|
91
|
+
expect(expectation.matches_method? :execute).to eq true
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
describe '#expected' do
|
97
|
+
|
98
|
+
let(:func) { MockFunction.new('func') }
|
99
|
+
|
100
|
+
it 'registers expect on #execute' do
|
101
|
+
expectation = func.expected
|
102
|
+
expect(expectation).to be_a Mocha::Expectation
|
103
|
+
expect(expectation.matches_method? :execute).to eq true
|
104
|
+
func.execute # satisfy the expect we just created on #execute!
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'clears rspec puppet cache' do
|
108
|
+
RSpec::Puppet::Support.expects(:clear_cache).once
|
109
|
+
func.expected
|
110
|
+
func.execute # satisfy the expect we just created on #execute!
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'works with parameter matchers' do
|
114
|
+
func.expected.with(regexp_matches(/thing/), anything)
|
115
|
+
scope.function_func ['something', 1234]
|
116
|
+
end
|
117
|
+
|
118
|
+
context 'when :keep_cache is set' do
|
119
|
+
|
120
|
+
it 'does not clear rspec puppet cache' do
|
121
|
+
RSpec::Puppet::Support.expects(:clear_cache).never
|
122
|
+
func.expected(:keep_cache)
|
123
|
+
func.execute # satisfy the expect we just created on #execute!
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
|
81
128
|
end
|
82
129
|
|
83
130
|
describe '#stub' do
|
84
131
|
|
85
132
|
let(:func) { MockFunction.new('func') }
|
86
133
|
|
87
|
-
it '
|
134
|
+
it 'stubs #call' do
|
88
135
|
expectation = func.stub
|
89
136
|
expect(expectation).to be_a Mocha::Expectation
|
90
137
|
expect(expectation.matches_method? :call).to eq true
|
@@ -96,14 +143,14 @@ describe MockFunction do
|
|
96
143
|
|
97
144
|
let(:func) { MockFunction.new('func') }
|
98
145
|
|
99
|
-
it '
|
146
|
+
it 'registers expect on #call' do
|
100
147
|
expectation = func.expect
|
101
148
|
expect(expectation).to be_a Mocha::Expectation
|
102
149
|
expect(expectation.matches_method? :call).to eq true
|
103
150
|
func.call [nil] # satisfy the expect we just created on #call!
|
104
151
|
end
|
105
152
|
|
106
|
-
it '
|
153
|
+
it 'clears rspec puppet cache' do
|
107
154
|
RSpec::Puppet::Support.expects(:clear_cache).once
|
108
155
|
func.expect
|
109
156
|
func.call [nil] # satisfy the expect we just created on #call!
|
@@ -111,7 +158,7 @@ describe MockFunction do
|
|
111
158
|
|
112
159
|
context 'when :keep_cache is set' do
|
113
160
|
|
114
|
-
it '
|
161
|
+
it 'does not clear rspec puppet cache' do
|
115
162
|
RSpec::Puppet::Support.expects(:clear_cache).never
|
116
163
|
func.expect(:keep_cache)
|
117
164
|
func.call [nil] # satisfy the expect we just created on #call!
|
@@ -125,13 +172,13 @@ describe MockFunction do
|
|
125
172
|
|
126
173
|
let!(:statement) { MockFunction.new 'statement', {:type => :statement} }
|
127
174
|
|
128
|
-
it '
|
175
|
+
it 'does not raise error' do
|
129
176
|
expect {
|
130
177
|
scope.function_statement []
|
131
178
|
}.to_not raise_error
|
132
179
|
end
|
133
180
|
|
134
|
-
it '
|
181
|
+
it 'responds to #call' do
|
135
182
|
expect(statement.respond_to? :call).to eq true
|
136
183
|
end
|
137
184
|
|
@@ -139,13 +186,13 @@ describe MockFunction do
|
|
139
186
|
|
140
187
|
context 'when :type => :rvalue' do
|
141
188
|
|
142
|
-
it '
|
189
|
+
it 'allows setup stubs' do
|
143
190
|
func = MockFunction.new('func') { |f| f.stubs(:call).returns('badger') }
|
144
191
|
result = func.call
|
145
192
|
expect(result).to eq 'badger'
|
146
193
|
end
|
147
194
|
|
148
|
-
it '
|
195
|
+
it 'returns values defined by a "let"' do
|
149
196
|
result = []
|
150
197
|
expect {
|
151
198
|
func = MockFunction.new('func') { |f| f.stubs(:call).returns(values_from_let) }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-puppet-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Poulton
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-07-28 00:00:00 Z
|
13
13
|
dependencies: []
|
14
14
|
|
15
15
|
description: Helper classes for mock/stub functions, templates and hierdata
|