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

Jruby + Rspec scoping issues on Classes with Prepended Modules #4896

Closed
NC-phuh opened this Issue Dec 15, 2017 · 2 comments

Comments

Projects
None yet
3 participants
@NC-phuh

NC-phuh commented Dec 15, 2017

Environment

  • jruby 9.1.13.0 (2.3.3) 2017-09-06 8e1c115 Java HotSpot(TM) 64-Bit Server VM 25.131-b11 on 1.8.0_131-b11 +jit [darwin-x86_64]
  • 16.7.0 Darwin Kernel Version 16.7.0: Thu Jun 15 17:36:27 PDT 2017; root:xnu-3789.70.16~2/RELEASE_X86_64 x86_64
  • rspec

I've created a sample project https://github.com/NC-phuh/jruby_rspec_bug that fully shows this issue.

Expected Behavior

module Prepender
end

class FakeClass
  prepend Prepender

  def fake_method(arg)
    return {}
  end
end

describe FakeClass do
  context "When a test stubs the prepended class" do
    it "should not persist this stub" do
      allow_any_instance_of(FakeClass)
        .to receive(:fake_method)
        .with("random argument")
        .and_return("returned value")
    end
  end

  context "when the class is no longer stubbed" do
    it "should not be affected by other tests" do
      expect(FakeClass.new.fake_method("non-random")).to eq({})
    end
  end
end
  • The above test passes in MRI Ruby.

Actual Behavior

  • The allow_any_instance_of is not getting cleared in jruby causing the second test to fail
Failures:

  1) FakeClass when the class is no longer stubbed should not be affected by other tests
     Failure/Error: expect(FakeClass.new.fake_method("non-random")).to eq({})

       #<FakeClass:0x74235045> received :fake_method with unexpected arguments
         expected: ("random argument")
              got: ("non-random")
        Please stub a default value first if message might be received with other args as well.
     # ./spec/rspec_scope_spec.rb:25:in `block in (root)'

Finished in 0.06384 seconds (files took 0.46047 seconds to load)
2 examples, 1 failure

It is important to note that this tests behaves as expected without a prepend Module statement.

I've traced this bug to https://github.com/rspec/rspec-mocks/blob/master/lib/rspec/mocks/any_instance/recorder.rb#L191

The line returns false even though both objects have the same id.
return unless @klass.instance_method(method_name).owner.to_s == @klass.to_s

@enebo enebo added this to the JRuby 9.1.16.0 milestone Dec 15, 2017

@headius

This comment has been minimized.

Show comment
Hide comment
@headius

headius Feb 12, 2018

Member

This appears to pass in 9.2 (master).

Member

headius commented Feb 12, 2018

This appears to pass in 9.2 (master).

@headius

This comment has been minimized.

Show comment
Hide comment
@headius

headius Feb 12, 2018

Member

Also passes in 9.1.16.0, probably due to recent prepend improvements.

Member

headius commented Feb 12, 2018

Also passes in 9.1.16.0, probably due to recent prepend improvements.

@headius headius closed this Feb 12, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment