rspec-armour 0.2.0 → 0.3.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/rspec/armour/checker.rb +14 -0
- data/lib/rspec/armour/receive_patch.rb +18 -5
- data/lib/rspec/armour/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: '09dba2766e980d9fb6994b653dead73b873fa386c5d44355403ccda5b5eace45'
|
|
4
|
+
data.tar.gz: 53f1869be85a9eec42007ca4f4fb46e54dad828aeb1a168d8ccf5974a4e853f6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 17ad1f31e987f44686609094ccd8b0dfc67dcd9ede6883198bb0a679b25c4a9b5a67c04442ead64d211edf741a0e7ecdae318b2a54d654b569e99cea53ca4b45
|
|
7
|
+
data.tar.gz: cff695382727e9ba569a49d6f8573ab8bee7de883d676c045b38a0a04506516d0f99444edf36234e121995501f1d535c53eef5dc494ff09103660b9e1aee1d04
|
data/CHANGELOG.md
CHANGED
data/lib/rspec/armour/checker.rb
CHANGED
|
@@ -21,9 +21,11 @@ module RSpec
|
|
|
21
21
|
].freeze
|
|
22
22
|
|
|
23
23
|
DISABLED_KEY = :rspec_armour_disabled
|
|
24
|
+
NEGATIVE_EXPECTATION_KEY = :rspec_armour_negative_expectation
|
|
24
25
|
|
|
25
26
|
def self.restricted?(target, method_name)
|
|
26
27
|
return false if disabled?
|
|
28
|
+
return false if negative_expectation?
|
|
27
29
|
|
|
28
30
|
method_sym = method_name.to_sym
|
|
29
31
|
is_class, klass = resolve_target_info(target)
|
|
@@ -34,6 +36,18 @@ module RSpec
|
|
|
34
36
|
method_sym) || association_method_on_class?(klass, method_sym)
|
|
35
37
|
end
|
|
36
38
|
|
|
39
|
+
def self.negative_expectation?
|
|
40
|
+
Thread.current[NEGATIVE_EXPECTATION_KEY]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.as_negative_expectation!
|
|
44
|
+
previous = Thread.current[NEGATIVE_EXPECTATION_KEY]
|
|
45
|
+
Thread.current[NEGATIVE_EXPECTATION_KEY] = true
|
|
46
|
+
yield
|
|
47
|
+
ensure
|
|
48
|
+
Thread.current[NEGATIVE_EXPECTATION_KEY] = previous
|
|
49
|
+
end
|
|
50
|
+
|
|
37
51
|
def self.resolve_target_info(target)
|
|
38
52
|
if test_double?(target)
|
|
39
53
|
resolve_double_info(target)
|
|
@@ -5,6 +5,12 @@ module RSpec
|
|
|
5
5
|
module Matchers
|
|
6
6
|
# This class is patched to prevent mocking ActiveRecord associations and finders.
|
|
7
7
|
class Receive
|
|
8
|
+
NEGATIVE_EXPECTATION_METHODS = %i[
|
|
9
|
+
setup_negative_expectation
|
|
10
|
+
setup_any_instance_negative_expectation
|
|
11
|
+
does_not_match?
|
|
12
|
+
].freeze
|
|
13
|
+
|
|
8
14
|
def self.patch_for_armour!
|
|
9
15
|
# rubocop:disable ThreadSafety/ClassInstanceVariable
|
|
10
16
|
@armour_patched ||= false
|
|
@@ -31,16 +37,23 @@ module RSpec
|
|
|
31
37
|
|
|
32
38
|
def self.patch_method(method)
|
|
33
39
|
original_method = instance_method(method)
|
|
40
|
+
negative = NEGATIVE_EXPECTATION_METHODS.include?(method)
|
|
34
41
|
define_method(method) do |subject, &block|
|
|
35
42
|
if ENV["RSPEC_ARMOUR_DEBUG"] == "true"
|
|
36
43
|
puts "Checking RSpec::Armour '#{@message}' on #{subject}"
|
|
37
44
|
end
|
|
38
|
-
if
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
45
|
+
if negative
|
|
46
|
+
RSpec::Armour::Checker.as_negative_expectation! do
|
|
47
|
+
original_method.bind_call(self, subject, &block)
|
|
48
|
+
end
|
|
49
|
+
else
|
|
50
|
+
if RSpec::Armour::Checker.restricted?(subject, @message)
|
|
51
|
+
message = "Mocking/stubbing ActiveRecord association or finder `#{@message}` " \
|
|
52
|
+
"on `#{subject.inspect}` is restricted by rspec-armour."
|
|
53
|
+
raise RSpec::Armour::MockError, message
|
|
54
|
+
end
|
|
55
|
+
original_method.bind_call(self, subject, &block)
|
|
42
56
|
end
|
|
43
|
-
original_method.bind_call(self, subject, &block)
|
|
44
57
|
end
|
|
45
58
|
end
|
|
46
59
|
end
|
data/lib/rspec/armour/version.rb
CHANGED