False negative on expectation of nested object method call, when it was mistakingly made private

8 views
Skip to first unread message

cy...@myjobglasses.com

unread,
Jul 5, 2018, 11:34:35 AM7/5/18
to rspec
Hello,

I just had a false negative (test reported as OK while feature was actually broken) when expecting a method call on a nested service object because the method was private, but after applying an `expect_any_instance_of` the method call was made public and caused the test to pass (despite calling `and_call_original`

Here is a sample way to repro

class Foo
 
private
 
 
def method_meant_to_be_public
 
end
end


class FooJob < ActiveJob::Base
 
def perform
   
Foo.new.method_meant_to_be_public
 
end
end



Performing a FooJob indeed causes a crash with

 private method `method_meant_to_be_public' called for #<Foo:0x00000003f58c98>


However my spec was, on first sight, not failing

describe FooJob do
  it
'triggers warmup failure for the pro' do
      expect_any_instance_of
(Foo).to receive(:method_meant_to_be_public).and_call_original
     
FooJob.perform_now
   
end
end


I am wondering if this is expected behavior or if I should report it as a bug ? It is not really obvious that using expect_any_instance_of would override the public/private status of methods...


Jon Rowe

unread,
Jul 5, 2018, 4:32:17 PM7/5/18
to rs...@googlegroups.com
Hello

Personally I would say that `and_call_original` here should check the method visibility, are you using verifying doubles? By stubbing the method you are making it exist even if it did not, but given the additional modifier I wonder if we can improve this.

Cheers
Jon

Jon Rowe
---------------------------

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/68928338-36a4-472c-8dd5-f685613e6750%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages