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

return_value matcher #91

Open
pirj opened this issue Jun 24, 2024 · 2 comments · May be fixed by rspec/rspec-expectations#1477
Open

return_value matcher #91

pirj opened this issue Jun 24, 2024 · 2 comments · May be fixed by rspec/rspec-expectations#1477
Assignees

Comments

@pirj
Copy link
Member

pirj commented Jun 24, 2024

I have a proof of concept of a matcher that works with block expectations, but checks the value returned by the block.
Works as a compound matcher, too:

$bar = 0
def foo
  $bar += 1
  2
end

RSpec.describe do
  it do
    $bar=0
    expect { foo }
      .to change { $bar }.to(1)
      .and return_value(2)
  end

  it do
    $bar=0
    expect { foo }
      .to return_value(satisfy { |x| x == 2 })
      .and change { $bar }.to(1)
  end
end

Existing known alternatives:

expect {
  expect(foo).to eq(2)
}.to change { $bar }.to(1)

PS it took me five years

Is this something worth continuing working on, WDYT @JonRowe ?

@pirj pirj self-assigned this Jun 24, 2024
@JonRowe
Copy link
Member

JonRowe commented Jun 28, 2024

I think I'd rather eq supported both block and value type of expectations e.g.

$bar=0
    expect { foo }
      .to change { $bar }.to(1)
      .and eq(2)

@pirj
Copy link
Member Author

pirj commented Jun 28, 2024

Maybe satisfy without/with a block?
That would allow passing composed matchers:

    $bar=0
    expect { foo }
      .to change { $bar }.to(1)
      .and satisfy(eq(2))

    $bar=0
    expect { foo }
      .to change { $bar }.to(1)
      .and satisfy { |baz| baz == 2 }

@pirj pirj closed this as completed Jun 28, 2024
@pirj pirj reopened this Jun 28, 2024
@JonRowe JonRowe transferred this issue from rspec/rspec-expectations Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants