# תמיכה ב-Ruby 3 הקשורה ל-gem mocha

**URL:** <https://meta.discourse.org/t/ruby-3-support-related-to-mocha-gem/205409>\
**Category:** Development\
**Created:** [7 באוקטובר,‏ 2021,‏ 3:41am UTC](https://meta.discourse.org/t/ruby-3-support-related-to-mocha-gem/205409 "2021-10-07T03:41:43Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![yahonda](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yahonda/32/234118_2.png) [@yahonda](https://meta.discourse.org/u/yahonda)\
**Post date:** [7 באוקטובר,‏ 2021,‏ 3:41am UTC](https://meta.discourse.org/t/ruby-3-support-related-to-mocha-gem/205409/1 "2021-10-07T03:41:43Z")

</div>

I have another topic to support Ruby 3.

### Steps to reproduce

```ruby
$ bundle exec rspec ./spec/lib/backup_restore/database_restorer_multisite_spec.rb:15

```

### Actual result with Ruby 3.0.2

```ruby
$ ruby -v
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]
$ bundle exec rspec ./spec/lib/backup_restore/database_restorer_multisite_spec.rb:15
Run options: include {:locations=>{"./spec/lib/backup_restore/database_restorer_multisite_spec.rb"=>[15]}}

Randomized with seed 43732
F

Failures:

  1) BackupRestore::DatabaseRestorer#restore database connection reconnects to the correct database
     Failure/Error:
       log Discourse::Utils.execute_command(
         {
           "SKIP_POST_DEPLOYMENT_MIGRATIONS" => "0",
           "SKIP_OPTIMIZE_ICONS" => "1",
           "DISABLE_TRANSLATION_OVERRIDES" => "1"
         },
         "rake", "db:migrate",
         failure_message: "Failed to migrate database.",
         chdir: Rails.root
       )

     Mocha::ExpectationError:
       unexpected invocation: Discourse::Utils.execute_command({"SKIP_POST_DEPLOYMENT_MIGRATIONS" => "0", "SKIP_OPTIMIZE_ICONS" => "1", "DISABLE_TRANSLATION_OVERRIDES" => "1"}, "rake", "db:migrate", {:failure_message => "Failed to migrate database.", :chdir => #<Pathname:0xd548>})
       unsatisfied expectations:
       - expected exactly once, invoked never: Discourse::Utils.execute_command()
       satisfied expectations:
       - expected exactly once, invoked once: BackupRestore.move_tables_between_schemas("public", "backup")
       - expected at least once, invoked 77 times: Migration::BaseDropper.create_readonly_function(any_parameters)
       - expected exactly once, invoked once: #<Mock:psql status>.exitstatus(any_parameters)
       - expected exactly once, invoked once: Process.last_status(any_parameters)
       - expected exactly twice, invoked twice: #<Mock:psql>.readline(any_parameters)
       - expected exactly once, invoked once: IO.popen(any_parameters)
     # ./lib/backup_restore/database_restorer.rb:138:in `migrate_database'
     # ./lib/backup_restore/database_restorer.rb:27:in `restore'
     # ./spec/lib/backup_restore/shared_context_for_backup_restore.rb:59:in `execute_stubbed_restore'
     # ./spec/lib/backup_restore/database_restorer_multisite_spec.rb:17:in `block (4 levels) in <top (required)>'
     # ./spec/rails_helper.rb:279:in `block (2 levels) in <top (required)>'

Finished in 0.28437 seconds (files took 2.37 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/lib/backup_restore/database_restorer_multisite_spec.rb:15 # BackupRestore::DatabaseRestorer#restore database connection reconnects to the correct database

Randomized with seed 43732

$

```

### Actual result with Ruby 2.7.4

```ruby
$ ruby -v
ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-linux]
$ bundle exec rspec ./spec/lib/backup_restore/database_restorer_multisite_spec.rb:15
Run options: include {:locations=>{"./spec/lib/backup_restore/database_restorer_multisite_spec.rb"=>[15]}}

Randomized with seed 37771
.

Finished in 0.63589 seconds (files took 2.63 seconds to load)
1 example, 0 failures

Randomized with seed 37771

$

```

### What I’ve found so far

Actually, this is another Ruby 3 keyword arguments support required.

- Execute with Ruby 2.7.4 enabling `-w` and find `warning: Using the last argument as keyword parameters is deprecated`

```ruby
$ bundle exec rspec -w ./spec/lib/backup_restore/database_restorer_multisite_spec.rb:15 2>&1 | grep keyword
/home/yahonda/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/mocha-1.13.0/lib/mocha/parameters_matcher.rb:13: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call

```

> <https://github.com/freerange/mocha/blob/191823e8696881ab8c72948165834eca18315cb3/lib/mocha/parameters_matcher.rb#L13>

- Here is the quick dirty fix with print debug code.

```diff
$ git diff 191823e8696881ab8c72948165834eca18315cb3...8141f2f0eca4bedcf59e34f31dd9ca35e39c2a25
diff --git a/lib/mocha/parameters_matcher.rb b/lib/mocha/parameters_matcher.rb
index 2b6ed84..26c4a25 100644
--- a/lib/mocha/parameters_matcher.rb
+++ b/lib/mocha/parameters_matcher.rb
@@ -10,7 +10,11 @@ module Mocha

     def match?(actual_parameters = [])
       if @matching_block
- @matching_block.call(*actual_parameters)
+ p actual_parameters
+ if actual_parameters.last.is_a? Hash
+ kwargs = actual_parameters.pop
+ end
+ @matching_block.call(*actual_parameters, **kwargs)
       else
         parameters_match?(actual_parameters)
       end
$

```

### Result with modified mocha code

```ruby
$ bundle exec rspec ./spec/lib/backup_restore/database_restorer_multisite_spec.rb:15
Run options: include {:locations=>{"./spec/lib/backup_restore/database_restorer_multisite_spec.rb"=>[15]}}

Randomized with seed 27573
[{"SKIP_POST_DEPLOYMENT_MIGRATIONS"=>"0", "SKIP_OPTIMIZE_ICONS"=>"1", "DISABLE_TRANSLATION_OVERRIDES"=>"1"}, "rake", "db:migrate", {:failure_message=>"Failed to migrate database.", :chdir=>#<Pathname:/home/yahonda/src/github.com/discourse/discourse>}]
.

Finished in 0.40263 seconds (files took 2.88 seconds to load)
1 example, 0 failures

Randomized with seed 27573

$

```

I may need to create a minimum case for mocha. It would be appreciated if someone has suggestions.

---

_[View the full topic](https://meta.discourse.org/t/ruby-3-support-related-to-mocha-gem/205409)._
