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

Array.clear empties the have_received expectation #1017

Closed
hypernova2002 opened this issue Sep 28, 2017 · 1 comment
Closed

Array.clear empties the have_received expectation #1017

hypernova2002 opened this issue Sep 28, 2017 · 1 comment

Comments

@hypernova2002
Copy link

I have a method which receives a non-empty array. I call array.clear after the method is called, but the expectation seems to believe the method was called with an empty array.


class TestClass
  def test_method_1(string)
    array = []
    array << string

    test_method_2(array)

    array.clear
  end

  def test_method_2(array); end
end

RSpec.describe TestClass do
  let(:test_class) { TestClass.new }
  let(:test_string) { 'some string' }

  it "method 1 calls method 2 with array" do
    allow(test_class).to receive(:test_method_2).with([test_string])

    test_class.test_method_1(test_string)

    expect(test_class).to have_received(:test_method_2).with([test_string])
  end
end
@JonRowe
Copy link
Member

JonRowe commented Oct 3, 2017

Congratulations, you found a bug! This is happening because the array is being mutated and we're not preventing that. See rspec/rspec-mocks#1183, closing as this is an rspec-mocks issue.

@JonRowe JonRowe closed this as completed Oct 3, 2017
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

No branches or pull requests

2 participants