tzu_mock 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tzu_mock.rb +5 -62
- data/lib/tzu_mock/class_methods.rb +16 -0
- data/lib/tzu_mock/configuration.rb +23 -0
- data/lib/tzu_mock/mocker.rb +48 -0
- data/spec/tzu_mock_spec.rb +20 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a374a7e23ecbd322adc0388add8d727efdaf0f39
|
4
|
+
data.tar.gz: d37787c1dc7664177fefa8e6e0b8d65f4a801106
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a8c33e3c20f176ef1125ce3c98c2a74f3d737d6736b27b0ef3317db0cf41743c5d6451ce61f5d7d78ba31f0278c0ac8645499ae4c273e2b6036a825c5fb94a4
|
7
|
+
data.tar.gz: 1760d50fcc90d31cff53ea5c449d56f1d0f85d6879e077912ad0dc1398a23641ee762a50d0c1200494d852728e4306e7a1d0a220135d78c56f78b4e938c05fe3
|
data/lib/tzu_mock.rb
CHANGED
@@ -1,65 +1,8 @@
|
|
1
1
|
require 'binding_of_caller'
|
2
|
+
require 'tzu_mock/class_methods'
|
3
|
+
require 'tzu_mock/mocker'
|
4
|
+
require 'tzu_mock/configuration'
|
2
5
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
class << self
|
7
|
-
def method_missing(method, *args)
|
8
|
-
return super(method) unless [:success, :invalid, :failure].include? method
|
9
|
-
prepare(method, *args)
|
10
|
-
end
|
11
|
-
|
12
|
-
def prepare(type, klass, result, method = nil)
|
13
|
-
# Get the rspec block context. Will not work if you call TzuMock#prepare directly.
|
14
|
-
# Call TzuMock#success, TzuMock#invalid, or TzuMock#failure instead
|
15
|
-
rspec_context = binding.of_caller(2).eval('self')
|
16
|
-
|
17
|
-
new(type, klass, result, rspec_context, method).mock
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def initialize(type, klass, result, rspec_context, method)
|
22
|
-
@type, @klass, @result, @rspec_context, @method = type, klass, result, rspec_context, method
|
23
|
-
end
|
24
|
-
|
25
|
-
def mock
|
26
|
-
@rspec_context.instance_eval(&mock_proc(@klass, mock_methods, success?, @result, error_type))
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
# Need to pass variables in explicity to give the Proc access to them
|
32
|
-
def mock_proc(klass, methods, success, result, type)
|
33
|
-
Proc.new do
|
34
|
-
methods.each do |method|
|
35
|
-
allow(klass).to receive(method) do |&block|
|
36
|
-
outcome = Tzu::Outcome.new(success, result, type)
|
37
|
-
outcome.handle(&block) if block
|
38
|
-
outcome
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def mock_methods
|
45
|
-
return [@method] if @method
|
46
|
-
MOCK_METHODS
|
47
|
-
end
|
48
|
-
|
49
|
-
def success?
|
50
|
-
@type == :success
|
51
|
-
end
|
52
|
-
|
53
|
-
def error_type
|
54
|
-
case @type
|
55
|
-
when :success
|
56
|
-
nil
|
57
|
-
when :invalid
|
58
|
-
:validation
|
59
|
-
when :failure
|
60
|
-
:execution
|
61
|
-
else
|
62
|
-
raise ArgumentError.new('Invalid type, must be :success, :invalid, or :failure')
|
63
|
-
end
|
64
|
-
end
|
6
|
+
module TzuMock
|
7
|
+
extend ClassMethods, Configuration
|
65
8
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module TzuMock
|
2
|
+
module ClassMethods
|
3
|
+
def method_missing(method, *args)
|
4
|
+
return super(method) unless [:success, :invalid, :failure].include? method
|
5
|
+
prepare(method, *args)
|
6
|
+
end
|
7
|
+
|
8
|
+
def prepare(type, klass, result, method = nil)
|
9
|
+
# Get the rspec block context. Will not work if you call TzuMock#prepare directly.
|
10
|
+
# Call TzuMock#success, TzuMock#invalid, or TzuMock#failure instead
|
11
|
+
rspec_context = binding.of_caller(2).eval('self')
|
12
|
+
|
13
|
+
Mocker.new(type, klass, result, rspec_context, method).mock
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module TzuMock
|
2
|
+
module Configuration
|
3
|
+
def configuration
|
4
|
+
@configuration ||= Config.new
|
5
|
+
end
|
6
|
+
|
7
|
+
def configure
|
8
|
+
yield(configuration)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Config
|
13
|
+
DEFAULT_MOCK_METHODS = [:run, :run!]
|
14
|
+
|
15
|
+
def stub_methods
|
16
|
+
@stub_methods ||= DEFAULT_MOCK_METHODS
|
17
|
+
end
|
18
|
+
|
19
|
+
def stub_methods=(methods)
|
20
|
+
@stub_methods = stub_methods + methods
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module TzuMock
|
2
|
+
class Mocker
|
3
|
+
def initialize(type, klass, result, rspec_context, method)
|
4
|
+
@type, @klass, @result, @rspec_context, @method = type, klass, result, rspec_context, method
|
5
|
+
end
|
6
|
+
|
7
|
+
def mock
|
8
|
+
@rspec_context.instance_eval(&mock_proc(@klass, mock_methods, success?, @result, error_type))
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
# Need to pass variables in explicity to give the Proc access to them
|
14
|
+
def mock_proc(klass, methods, success, result, type)
|
15
|
+
Proc.new do
|
16
|
+
methods.each do |method|
|
17
|
+
allow(klass).to receive(method) do |&block|
|
18
|
+
outcome = Tzu::Outcome.new(success, result, type)
|
19
|
+
outcome.handle(&block) if block
|
20
|
+
outcome
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def mock_methods
|
27
|
+
return [@method] if @method
|
28
|
+
TzuMock.configuration.stub_methods
|
29
|
+
end
|
30
|
+
|
31
|
+
def success?
|
32
|
+
@type == :success
|
33
|
+
end
|
34
|
+
|
35
|
+
def error_type
|
36
|
+
case @type
|
37
|
+
when :success
|
38
|
+
nil
|
39
|
+
when :invalid
|
40
|
+
:validation
|
41
|
+
when :failure
|
42
|
+
:execution
|
43
|
+
else
|
44
|
+
raise ArgumentError.new('Invalid type, must be :success, :invalid, or :failure')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/spec/tzu_mock_spec.rb
CHANGED
@@ -34,6 +34,26 @@ describe TzuMock do
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
+
context 'configuration' do
|
38
|
+
context 'when no stub methods have been added' do
|
39
|
+
it 'configures the default stub methods' do
|
40
|
+
expect(TzuMock.configuration.stub_methods).to eq TzuMock::Config::DEFAULT_MOCK_METHODS
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'when stub methods have been added' do
|
45
|
+
let(:stub_methods) { [:go, :go!] }
|
46
|
+
|
47
|
+
before do
|
48
|
+
TzuMock.configure { |config| config.stub_methods = stub_methods }
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'appends the configured methods to the default stub methods' do
|
52
|
+
expect(TzuMock.configuration.stub_methods.sort).to eq (TzuMock::Config::DEFAULT_MOCK_METHODS + stub_methods).sort
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
37
57
|
context 'when invocation does not have a block' do
|
38
58
|
context 'success' do
|
39
59
|
before { TzuMock.success(UpdateUser, result) }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tzu_mock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Blake Turner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: binding_of_caller
|
@@ -75,6 +75,9 @@ files:
|
|
75
75
|
- LICENSE.txt
|
76
76
|
- README.md
|
77
77
|
- lib/tzu_mock.rb
|
78
|
+
- lib/tzu_mock/class_methods.rb
|
79
|
+
- lib/tzu_mock/configuration.rb
|
80
|
+
- lib/tzu_mock/mocker.rb
|
78
81
|
- spec/spec_helper.rb
|
79
82
|
- spec/tzu_mock_spec.rb
|
80
83
|
homepage: https://github.com/onfido/tzu_mock
|