expect-call 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/expect-call.rb +45 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8665a08b1332f778b203b8bcf996d40083dc1a44
4
+ data.tar.gz: 387a04a8b8425c60c60b4e272dc66ad1fcd5aa81
5
+ SHA512:
6
+ metadata.gz: 382dbd8558701953311cd43453319f658c131bfb72974e6c6b1b268028274f2f5945494f646fd5c42292a2d45dca4834f2e448f09ffe22b958b608e10c241ade
7
+ data.tar.gz: 74f537b8c609cb01b39ea4e8aec6c308383b042edc28edb47dfa7c6bd17af7d06c283b068d74117e915baf7fe32df63fb1c7c467eea03ac200cf4e4b00ef864b
@@ -0,0 +1,45 @@
1
+ class Object
2
+ #
3
+ # Partial mocking.
4
+ # Stubs a regular object and expects a specific call on it.
5
+ #
6
+ def expect_call( method_name, params, val_or_callable, &block )
7
+ assert_method = block.binding.eval( 'self' ).method( :assert )
8
+
9
+ metaclass = class << self; self; end
10
+
11
+ orig_method = nil
12
+ orig_method = method( method_name ) if respond_to? method_name
13
+
14
+ clean_up = Proc.new do
15
+ metaclass.send :undef_method, method_name
16
+ if orig_method
17
+ metaclass.send :define_method, method_name, orig_method
18
+ end
19
+ end
20
+
21
+ was_called = false
22
+
23
+ new_method = Proc.new do |*args|
24
+ was_called = true
25
+ clean_up.call
26
+
27
+ assert_method.call params == args,
28
+ "Params do not match\n Expected: %s\n Actual: %s" % [ params.inspect, args.inspect ]
29
+
30
+ if val_or_callable.respond_to?( :call )
31
+ return val_or_callable.call( *args )
32
+ end
33
+
34
+ val_or_callable
35
+ end
36
+
37
+ metaclass.send :define_method, method_name, new_method
38
+
39
+ yield
40
+
41
+ assert_method.call was_called, "Was not called: #{ method_name }"
42
+ ensure
43
+ clean_up.call unless was_called
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: expect-call
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Shubin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Stubs a regular object's method and expects a specific call to it.
14
+ email: andrey.shubin@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/expect-call.rb
20
+ homepage: https://github.com/d-ash/expect-call
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.5.1
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Partial mocking (expect a method call)
44
+ test_files: []