Skip to content

Commit

Permalink
refactor stubs (#6)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Ermolaev Andrey <[email protected]>
  • Loading branch information
xronos-i-am and ermolaev authored Jan 31, 2025
1 parent 20ea63a commit 03df7bb
Show file tree
Hide file tree
Showing 20 changed files with 331 additions and 213 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ jobs:
strategy:
matrix:
ruby:
- '3.0.6'
- '3.1.4'
- '3.2.2'
- '3.1.6'
- '3.2.6'
- '3.3.7'
- '3.4.1'

steps:
- uses: actions/checkout@v3
Expand Down
8 changes: 7 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ Gp/ModuleMethodInWrongFile:
- lib/active_dry_deps/stub.rb

AllCops:
TargetRubyVersion: 3.0
TargetRubyVersion: 3.0

Layout/EmptyLinesAroundClassBody:
Enabled: false

Layout/EmptyLinesAroundModuleBody:
Enabled: false
14 changes: 7 additions & 7 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# frozen_string_literal: true

source 'https://rubygems.org'
source "https://rubygems.org"

gemspec

gem 'rake', '~> 13.0'
gem "rake", "~> 13.0"

gem 'rspec', '~> 3.0'
gem "rspec", "~> 3.0"

gem 'activesupport'
gem 'combustion'
gem 'railties'
gem 'rubocop-gp', github: 'corp-gp/rubocop-gp', require: false
gem "activesupport", "~> 7.2"
gem "combustion"
gem "railties", "~> 7.2"
gem "rubocop-gp", github: "corp-gp/rubocop-gp", require: false
210 changes: 128 additions & 82 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
GIT
remote: https://github.com/corp-gp/rubocop-gp.git
revision: 94d07b3b370a199f7d26094d3606d79a0cf92f57
revision: 867f7e1351c3730897cacdc20ce2d727604de245
specs:
rubocop-gp (0.0.3)
rubocop-gp (0.0.4)
rubocop
rubocop-capybara
rubocop-factory_bot
rubocop-performance
rubocop-rails
rubocop-rspec
rubocop-rspec_rails

PATH
remote: .
Expand All @@ -17,138 +20,181 @@ PATH
GEM
remote: https://rubygems.org/
specs:
actionpack (7.0.7)
actionview (= 7.0.7)
activesupport (= 7.0.7)
rack (~> 2.0, >= 2.2.4)
actionpack (7.2.2.1)
actionview (= 7.2.2.1)
activesupport (= 7.2.2.1)
nokogiri (>= 1.8.5)
racc
rack (>= 2.2.4, < 3.2)
rack-session (>= 1.0.1)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actionview (7.0.7)
activesupport (= 7.0.7)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
useragent (~> 0.16)
actionview (7.2.2.1)
activesupport (= 7.2.2.1)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activesupport (7.0.7)
concurrent-ruby (~> 1.0, >= 1.0.2)
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
activesupport (7.2.2.1)
base64
benchmark (>= 0.3)
bigdecimal
concurrent-ruby (~> 1.0, >= 1.3.1)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
logger (>= 1.4.2)
minitest (>= 5.1)
tzinfo (~> 2.0)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
ast (2.4.2)
base64 (0.1.1)
base64 (0.2.0)
benchmark (0.4.0)
bigdecimal (3.1.9)
builder (3.3.0)
combustion (1.5.0)
activesupport (>= 3.0.0)
railties (>= 3.0.0)
thor (>= 0.14.6)
concurrent-ruby (1.2.2)
concurrent-ruby (1.3.5)
connection_pool (2.5.0)
crass (1.0.6)
diff-lcs (1.5.0)
dry-configurable (1.1.0)
dry-core (~> 1.0, < 2)
date (3.4.1)
diff-lcs (1.5.1)
drb (2.2.1)
dry-configurable (1.3.0)
dry-core (~> 1.1)
zeitwerk (~> 2.6)
dry-core (1.0.1)
dry-core (1.1.0)
concurrent-ruby (~> 1.0)
logger
zeitwerk (~> 2.6)
erubi (1.13.0)
i18n (1.14.1)
erubi (1.13.1)
i18n (1.14.7)
concurrent-ruby (~> 1.0)
json (2.6.3)
language_server-protocol (3.17.0.3)
loofah (2.23.1)
io-console (0.8.0)
irb (1.15.1)
pp (>= 0.6.0)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.9.1)
language_server-protocol (3.17.0.4)
logger (1.6.5)
loofah (2.24.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
method_source (1.1.0)
minitest (5.19.0)
nokogiri (1.16.8-arm64-darwin)
minitest (5.25.4)
nokogiri (1.18.2-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.8-x86_64-linux)
nokogiri (1.18.2-x86_64-linux-gnu)
racc (~> 1.4)
parallel (1.23.0)
parser (3.2.2.3)
parallel (1.26.3)
parser (3.3.7.0)
ast (~> 2.4.1)
racc
racc (1.7.1)
rack (2.2.8)
rack-test (2.1.0)
pp (0.6.2)
prettyprint
prettyprint (0.2.0)
psych (5.2.3)
date
stringio
racc (1.8.1)
rack (3.1.8)
rack-session (2.1.0)
base64 (>= 0.1.0)
rack (>= 3.0.0)
rack-test (2.2.0)
rack (>= 1.3)
rackup (2.2.1)
rack (>= 3)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.6.1)
rails-html-sanitizer (1.6.2)
loofah (~> 2.21)
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
railties (7.0.7)
actionpack (= 7.0.7)
activesupport (= 7.0.7)
method_source
railties (7.2.2.1)
actionpack (= 7.2.2.1)
activesupport (= 7.2.2.1)
irb (~> 1.13)
rackup (>= 1.0.0)
rake (>= 12.2)
thor (~> 1.0)
zeitwerk (~> 2.5)
thor (~> 1.0, >= 1.2.2)
zeitwerk (~> 2.6)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.8.1)
rexml (3.2.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
rake (13.2.1)
rdoc (6.11.0)
psych (>= 4.0.0)
regexp_parser (2.10.0)
reline (0.6.0)
io-console (~> 0.5)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.2)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.6)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
rubocop (1.56.0)
base64 (~> 0.1.1)
rspec-support (~> 3.13.0)
rspec-support (3.13.2)
rubocop (1.71.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.2.2.3)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.28.1, < 2.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.36.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
rubocop-capybara (2.18.0)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.38.0)
parser (>= 3.3.1.0)
rubocop-capybara (2.21.0)
rubocop (~> 1.41)
rubocop-factory_bot (2.23.1)
rubocop (~> 1.33)
rubocop-performance (1.19.0)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rails (2.20.2)
rubocop-factory_bot (2.26.1)
rubocop (~> 1.61)
rubocop-performance (1.23.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rails (2.29.1)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)
rubocop-rspec (2.23.2)
rubocop (~> 1.33)
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
rubocop (>= 1.52.0, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rspec (3.4.0)
rubocop (~> 1.61)
rubocop-rspec_rails (2.30.0)
rubocop (~> 1.61)
rubocop-rspec (~> 3, >= 3.0.1)
ruby-progressbar (1.13.0)
securerandom (0.4.1)
stringio (3.1.2)
thor (1.3.2)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)
zeitwerk (2.6.11)
unicode-display_width (3.1.4)
unicode-emoji (~> 4.0, >= 4.0.4)
unicode-emoji (4.0.4)
useragent (0.16.11)
zeitwerk (2.6.18)

PLATFORMS
arm64-darwin-21
arm64-darwin-24
x86_64-linux

DEPENDENCIES
active_dry_deps!
activesupport
activesupport (~> 7.2)
combustion
railties
railties (~> 7.2)
rake (~> 13.0)
rspec (~> 3.0)
rubocop-gp!
Expand Down
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,17 @@ require 'active_dry_deps/rspec'
require 'active_dry_deps/stub'

Deps.enable_stubs!

RSpec.configure do |config|
config.after(:each) { Deps.reset }
end
```

#### deps
The gem adds Rspec matcher `deps` for stub dependency

```ruby
Deps.register('order.dependency') { Class.new { def self.call = 'failure' } }
Deps.register('order.dependency', Class.new { def self.call = 'failure' })

let(:service_klass) do
Class.new do
Expand All @@ -168,19 +172,42 @@ it 'success' do
end
```

#### stub, unstub
#### stub, unstub, reset
Dependency can be stubbed at the container level. This allows to override all calls to it

```ruby
it 'stub' do
Deps.stub('Order::Dependency', double(call: 'success'))
expect(service_klass.new.call).to be 'success'

Deps.unstub('Order::Dependency') # or Deps.unstub() for unsub all keys
Deps.unstub('Order::Dependency') # or Deps.reset for unsub all keys
expect(service_klass.new.call).to be 'failure'
end
```

#### global_stub, global_unstub
Sometimes it is necessary to stub dependencies for all or almost all tests

# spec/rails_helper.rb
```ruby
# ...
Deps.enable_stubs!

Deps.global_stub('PushService', Class.new { def self.call = 'global-stub-push' })
```

Dependency stubbed with `global_stub` may be restored only with `global_unstub`. You can unstub dependency when it really needed and ignore in all other cases

```ruby
it 'sends webpush' do
Deps.global_unstub('PushService')

# expect(PushService.call).to ...
end
```

*`Deps.global_stub` should not be used within examples*

## Configuration
The gem is auto-configuring, but you can override settings

Expand Down
Loading

0 comments on commit 03df7bb

Please sign in to comment.