rspec-armour 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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +55 -0
- data/Rakefile +12 -0
- data/lib/rspec/armour/checker.rb +102 -0
- data/lib/rspec/armour/receive_patch.rb +49 -0
- data/lib/rspec/armour/version.rb +7 -0
- data/lib/rspec/armour.rb +30 -0
- data/quality.sh +11 -0
- metadata +183 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 3cd64c8d03babf2b9a79453d2c6c2eaa62126ad2880135e73c03fe6d6f56e824
|
|
4
|
+
data.tar.gz: 91a57cfa13c6a701acc53ee7281a9cf64608c4dd4fe87f96d0290d0a575bf8ca
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c29cbe69ed17b4e69bfbbf62e3e79545e789c04ee8f8ba827cce9bcb91334bcaae3732a86716d37ad8e89de00aa355a6089b1e805a8415b03d0c34ff34742d88
|
|
7
|
+
data.tar.gz: 21cf05dc106bf0f06ea4ae60f3625f5a6e61c210e037ba0654670e6f89e3a20c471b159e43e5470e9b4820dadea48e3eff8a86cc5fa4086b92c22776ac89c137
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Harry Lascelles
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# rspec-armour
|
|
2
|
+
|
|
3
|
+
`rspec-armour` protects your ActiveRecord models from being mocked or stubbed in RSpec tests. It forces you to use real database objects (or factories) for your models, leading to more robust and reliable tests.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'rspec-armour'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle install
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install rspec-armour
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
Once required, `rspec-armour` automatically prevents mocking of ActiveRecord finders, associations, and persistence methods.
|
|
24
|
+
|
|
25
|
+
### Examples of restricted methods:
|
|
26
|
+
|
|
27
|
+
* **Class methods:** `.find`, `.where`, `.create`, `.update`, etc.
|
|
28
|
+
* **Instance methods:** `#save`, `#update`, `#destroy`, `#reload`, etc.
|
|
29
|
+
* **Associations:** `has_many`, `belongs_to`, and their helper methods (e.g., `user.items`, `user.item_ids`).
|
|
30
|
+
|
|
31
|
+
### Disabling restrictions
|
|
32
|
+
|
|
33
|
+
If you absolutely must mock an ActiveRecord method, you can do so by wrapping the code in a block:
|
|
34
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
RSpec::Armour.without_restrictions do
|
|
37
|
+
allow(User).to receive(:where).and_return([])
|
|
38
|
+
end
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Or by using RSpec metadata:
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
it "does something specific", :without_rspec_armour do
|
|
45
|
+
allow(User).to receive(:where).and_return([])
|
|
46
|
+
end
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Contributing
|
|
50
|
+
|
|
51
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hlascelles/rspec-armour.
|
|
52
|
+
|
|
53
|
+
## License
|
|
54
|
+
|
|
55
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_record"
|
|
4
|
+
require "active_support/core_ext/string/inflections"
|
|
5
|
+
|
|
6
|
+
module RSpec
|
|
7
|
+
module Armour
|
|
8
|
+
module Checker
|
|
9
|
+
RESTRICTED_CLASS_METHODS = %i[
|
|
10
|
+
find find_by find_by! where all first last count sum average minimum
|
|
11
|
+
maximum pluck pick exists? create create! find_or_create_by
|
|
12
|
+
find_or_create_by! find_or_initialize_by update update! destroy_all
|
|
13
|
+
delete_all delete_by destroy_by first_or_create first_or_create!
|
|
14
|
+
first_or_initialize
|
|
15
|
+
].freeze
|
|
16
|
+
|
|
17
|
+
RESTRICTED_INSTANCE_METHODS = %i[
|
|
18
|
+
save save! update update! update_attribute update_columns update_column
|
|
19
|
+
destroy destroy! delete delete! touch reload increment! decrement!
|
|
20
|
+
toggle!
|
|
21
|
+
].freeze
|
|
22
|
+
|
|
23
|
+
DISABLED_KEY = :rspec_armour_disabled
|
|
24
|
+
|
|
25
|
+
def self.restricted?(target, method_name)
|
|
26
|
+
return false if disabled?
|
|
27
|
+
|
|
28
|
+
method_sym = method_name.to_sym
|
|
29
|
+
is_class, klass = resolve_target_info(target)
|
|
30
|
+
|
|
31
|
+
return false unless klass.is_a?(Class) && klass < ::ActiveRecord::Base
|
|
32
|
+
|
|
33
|
+
restricted_by_list?(is_class,
|
|
34
|
+
method_sym) || association_method_on_class?(klass, method_sym)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.resolve_target_info(target)
|
|
38
|
+
if test_double?(target)
|
|
39
|
+
resolve_double_info(target)
|
|
40
|
+
else
|
|
41
|
+
is_class = target.is_a?(Class)
|
|
42
|
+
[is_class, is_class ? target : target.class]
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.test_double?(target)
|
|
47
|
+
defined?(::RSpec::Mocks::TestDouble) && target.is_a?(::RSpec::Mocks::TestDouble)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def self.resolve_double_info(target)
|
|
51
|
+
ref = target.instance_variable_get(:@doubled_module)
|
|
52
|
+
return [false, nil] unless ref.respond_to?(:target)
|
|
53
|
+
|
|
54
|
+
underlying_target = ref.target
|
|
55
|
+
case target
|
|
56
|
+
when ::RSpec::Mocks::InstanceVerifyingDouble
|
|
57
|
+
[false, underlying_target]
|
|
58
|
+
when ::RSpec::Mocks::ClassVerifyingDouble
|
|
59
|
+
[true, underlying_target]
|
|
60
|
+
when ::RSpec::Mocks::ObjectVerifyingDouble
|
|
61
|
+
is_class = underlying_target.is_a?(Class)
|
|
62
|
+
[is_class, is_class ? underlying_target : underlying_target.class]
|
|
63
|
+
else
|
|
64
|
+
[false, nil]
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def self.restricted_by_list?(is_class, method_sym)
|
|
69
|
+
return true if RESTRICTED_INSTANCE_METHODS.include?(method_sym)
|
|
70
|
+
|
|
71
|
+
is_class && RESTRICTED_CLASS_METHODS.include?(method_sym)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def self.association_method_on_class?(klass, method_name)
|
|
75
|
+
return false unless klass.respond_to?(:reflections)
|
|
76
|
+
|
|
77
|
+
reflections = klass.reflections
|
|
78
|
+
reflections.any? do |name, _reflection|
|
|
79
|
+
singular_name = name.to_s.singularize
|
|
80
|
+
[
|
|
81
|
+
name.to_sym,
|
|
82
|
+
:"#{name}=",
|
|
83
|
+
:"#{singular_name}_ids",
|
|
84
|
+
:"#{singular_name}_ids=",
|
|
85
|
+
].include?(method_name)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def self.disabled?
|
|
90
|
+
Thread.current[DISABLED_KEY]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def self.disable!
|
|
94
|
+
previous = Thread.current[DISABLED_KEY]
|
|
95
|
+
Thread.current[DISABLED_KEY] = true
|
|
96
|
+
yield
|
|
97
|
+
ensure
|
|
98
|
+
Thread.current[DISABLED_KEY] = previous
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RSpec
|
|
4
|
+
module Mocks
|
|
5
|
+
module Matchers
|
|
6
|
+
# This class is patched to prevent mocking ActiveRecord associations and finders.
|
|
7
|
+
class Receive
|
|
8
|
+
def self.patch_for_armour!
|
|
9
|
+
# rubocop:disable ThreadSafety/ClassInstanceVariable
|
|
10
|
+
@armour_patched ||= false
|
|
11
|
+
return if @armour_patched
|
|
12
|
+
|
|
13
|
+
@armour_patched = true
|
|
14
|
+
# rubocop:enable ThreadSafety/ClassInstanceVariable
|
|
15
|
+
|
|
16
|
+
%i[
|
|
17
|
+
setup_expectation
|
|
18
|
+
setup_negative_expectation
|
|
19
|
+
setup_allowance
|
|
20
|
+
setup_any_instance_expectation
|
|
21
|
+
setup_any_instance_negative_expectation
|
|
22
|
+
setup_any_instance_allowance
|
|
23
|
+
matches?
|
|
24
|
+
does_not_match?
|
|
25
|
+
].each do |method|
|
|
26
|
+
next unless method_defined?(method)
|
|
27
|
+
|
|
28
|
+
patch_method(method)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.patch_method(method)
|
|
33
|
+
original_method = instance_method(method)
|
|
34
|
+
define_method(method) do |subject, &block|
|
|
35
|
+
if ENV["RSPEC_ARMOUR_DEBUG"] == "true"
|
|
36
|
+
puts "Checking RSpec::Armour '#{@message}' on #{subject}"
|
|
37
|
+
end
|
|
38
|
+
if RSpec::Armour::Checker.restricted?(subject, @message)
|
|
39
|
+
message = "Mocking/stubbing ActiveRecord association or finder `#{@message}` " \
|
|
40
|
+
"on `#{subject.inspect}` is restricted by rspec-armour."
|
|
41
|
+
raise RSpec::Armour::MockError, message
|
|
42
|
+
end
|
|
43
|
+
original_method.bind_call(self, subject, &block)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
data/lib/rspec/armour.rb
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rspec"
|
|
4
|
+
require "rspec/mocks"
|
|
5
|
+
require_relative "armour/version"
|
|
6
|
+
require_relative "armour/checker"
|
|
7
|
+
require_relative "armour/receive_patch"
|
|
8
|
+
|
|
9
|
+
module RSpec
|
|
10
|
+
module Armour
|
|
11
|
+
class MockError < StandardError; end
|
|
12
|
+
|
|
13
|
+
def self.without_restrictions(&)
|
|
14
|
+
Checker.disable!(&)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Ensure the matcher is loaded
|
|
20
|
+
require "rspec/mocks/matchers/receive"
|
|
21
|
+
|
|
22
|
+
RSpec::Mocks::Matchers::Receive.patch_for_armour!
|
|
23
|
+
|
|
24
|
+
RSpec.configure do |config|
|
|
25
|
+
config.around(:each, without_rspec_armour: true) do |example|
|
|
26
|
+
RSpec::Armour.without_restrictions do
|
|
27
|
+
example.run
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
data/quality.sh
ADDED
metadata
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rspec-armour
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Harry Lascelles
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: activerecord
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: rspec-mocks
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: rubocop-magic_numbers
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: rubocop-performance
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0'
|
|
68
|
+
- !ruby/object:Gem::Dependency
|
|
69
|
+
name: rubocop-rails
|
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
type: :development
|
|
76
|
+
prerelease: false
|
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
- !ruby/object:Gem::Dependency
|
|
83
|
+
name: rubocop-rake
|
|
84
|
+
requirement: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '0'
|
|
89
|
+
type: :development
|
|
90
|
+
prerelease: false
|
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
- !ruby/object:Gem::Dependency
|
|
97
|
+
name: rubocop-rspec
|
|
98
|
+
requirement: !ruby/object:Gem::Requirement
|
|
99
|
+
requirements:
|
|
100
|
+
- - ">="
|
|
101
|
+
- !ruby/object:Gem::Version
|
|
102
|
+
version: '0'
|
|
103
|
+
type: :development
|
|
104
|
+
prerelease: false
|
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
106
|
+
requirements:
|
|
107
|
+
- - ">="
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: '0'
|
|
110
|
+
- !ruby/object:Gem::Dependency
|
|
111
|
+
name: rubocop-thread_safety
|
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
|
113
|
+
requirements:
|
|
114
|
+
- - ">="
|
|
115
|
+
- !ruby/object:Gem::Version
|
|
116
|
+
version: '0'
|
|
117
|
+
type: :development
|
|
118
|
+
prerelease: false
|
|
119
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
120
|
+
requirements:
|
|
121
|
+
- - ">="
|
|
122
|
+
- !ruby/object:Gem::Version
|
|
123
|
+
version: '0'
|
|
124
|
+
- !ruby/object:Gem::Dependency
|
|
125
|
+
name: sqlite3
|
|
126
|
+
requirement: !ruby/object:Gem::Requirement
|
|
127
|
+
requirements:
|
|
128
|
+
- - ">="
|
|
129
|
+
- !ruby/object:Gem::Version
|
|
130
|
+
version: '0'
|
|
131
|
+
type: :development
|
|
132
|
+
prerelease: false
|
|
133
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
134
|
+
requirements:
|
|
135
|
+
- - ">="
|
|
136
|
+
- !ruby/object:Gem::Version
|
|
137
|
+
version: '0'
|
|
138
|
+
description: 'rspec-armour prevents developers from mocking ActiveRecord finders,
|
|
139
|
+
associations, and persistence methods, ensuring tests use real database interactions.
|
|
140
|
+
|
|
141
|
+
'
|
|
142
|
+
email:
|
|
143
|
+
- harry@harryl.com
|
|
144
|
+
executables: []
|
|
145
|
+
extensions: []
|
|
146
|
+
extra_rdoc_files: []
|
|
147
|
+
files:
|
|
148
|
+
- CHANGELOG.md
|
|
149
|
+
- LICENSE.txt
|
|
150
|
+
- README.md
|
|
151
|
+
- Rakefile
|
|
152
|
+
- lib/rspec/armour.rb
|
|
153
|
+
- lib/rspec/armour/checker.rb
|
|
154
|
+
- lib/rspec/armour/receive_patch.rb
|
|
155
|
+
- lib/rspec/armour/version.rb
|
|
156
|
+
- quality.sh
|
|
157
|
+
homepage: https://github.com/hlascelles/rspec-armour
|
|
158
|
+
licenses:
|
|
159
|
+
- MIT
|
|
160
|
+
metadata:
|
|
161
|
+
allowed_push_host: https://rubygems.org
|
|
162
|
+
homepage_uri: https://github.com/hlascelles/rspec-armour
|
|
163
|
+
source_code_uri: https://github.com/hlascelles/rspec-armour
|
|
164
|
+
changelog_uri: https://github.com/hlascelles/rspec-armour/blob/main/CHANGELOG.md
|
|
165
|
+
rubygems_mfa_required: 'true'
|
|
166
|
+
rdoc_options: []
|
|
167
|
+
require_paths:
|
|
168
|
+
- lib
|
|
169
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
170
|
+
requirements:
|
|
171
|
+
- - ">="
|
|
172
|
+
- !ruby/object:Gem::Version
|
|
173
|
+
version: 3.2.0
|
|
174
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
175
|
+
requirements:
|
|
176
|
+
- - ">="
|
|
177
|
+
- !ruby/object:Gem::Version
|
|
178
|
+
version: '0'
|
|
179
|
+
requirements: []
|
|
180
|
+
rubygems_version: 4.0.6
|
|
181
|
+
specification_version: 4
|
|
182
|
+
summary: Restrict mocking and stubbing of ActiveRecord models in RSpec.
|
|
183
|
+
test_files: []
|