Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RSpec mock expectations for Ruby 3 #816

Merged
merged 1 commit into from
Feb 7, 2022
Merged

Conversation

tombruijn
Copy link
Member

Something changed in the rspec-mocks gem between versions 3.10.2
and 3.10.3, because that's what causes tests to fail like this
on Ruby 3.1.0 and 3.0.3 locally and on the CI:

Failure/Error: monitor_transaction(name, env, &block)

  Appsignal received :monitor_transaction with unexpected arguments
    expected: ("perform_job.something", {:key=>:value})
         got: ("perform_job.something", {:key=>:value})
  Diff:

In this example the arguments are exactly the same, except they're not
the identical objects. This particular issue seems to be caused by a
recent change that for Ruby 3 keyword argument matching:
rspec/rspec-mocks#1460

Rewriting specs

What helps is rewriting the tests to not mock method calls and check the
arguments given. Instead, assert what's being stored on the
transactions, like how we want all tests to be written as described in
issue #252.

Adding curly brackets

For other specs that are a little more difficult to rewrite this way
right now, I've chosen to add additional curly brackets around the
argument expectations so that it's clear to Ruby/RSpec it's a hash
argument and not keywords arguments.

To solve this more permanently, we should rewrite the specs at some
point. We can remove these curly brackets if the issue is fixed in a
future rspec-mocks version as well.

[skip changeset]

Something changed in the rspec-mocks gem between versions 3.10.2
and 3.10.3, because that's what causes tests to fail like this
on Ruby 3.1.0 and 3.0.3 locally and on the CI:

```
Failure/Error: monitor_transaction(name, env, &block)

  Appsignal received :monitor_transaction with unexpected arguments
    expected: ("perform_job.something", {:key=>:value})
         got: ("perform_job.something", {:key=>:value})
  Diff:
```

In this example the arguments are exactly the same, except they're not
the identical objects. This particular issue seems to be caused by a
recent change that for Ruby 3 keyword argument matching:
rspec/rspec-mocks#1460

## Rewriting specs

What helps is rewriting the tests to not mock method calls and check the
arguments given. Instead, assert what's being stored on the
transactions, like how we want all tests to be written as described in
issue #252.

## Adding curly brackets

For other specs that are a little more difficult to rewrite this way
right now, I've chosen to add additional curly brackets around the
argument expectations so that it's clear to Ruby/RSpec it's a hash
argument and not keywords arguments.

To solve this more permanently, we should rewrite the specs at some
point. We can remove these curly brackets if the issue is fixed in a
future rspec-mocks version as well.

[skip changeset]
@tombruijn tombruijn merged commit 0e9b72f into main Feb 7, 2022
@tombruijn tombruijn deleted the fix-rspec-expectations branch July 31, 2023 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants