how to stub private methods ?

5,938 views
Skip to first unread message

Arup Rakshit

unread,
Jul 3, 2014, 9:10:51 AM7/3/14
to rs...@googlegroups.com
I have the below code :

class Foo
  def intialize
    #
  end

  def meth
    OpenStruct.new(val : meth1, val: meth2)
  end

  private

  def meth1
    #..
  end

  def meth2
    #..
  end
end

How can I stub those 2 private methods *meth1* and *meth2*.  *should* wouldn't work for me, as mentioned here - http://stackoverflow.com/questions/14987141/rspec-stub-private-method



Aaron Kromer

unread,
Jul 3, 2014, 12:19:56 PM7/3/14
to rs...@googlegroups.com

Arup,

Can you post your spec and exception. These should work on 2.14. If you are on RSpec 3, you need to either switch to the new syntax (https://www.relishapp.com/rspec/rspec-mocks/v/3-0/docs/basics/allowing-messages) or configure the old syntax (https://www.relishapp.com/rspec/rspec-mocks/v/3-0/docs/old-syntax).

Here is a sample with the new syntax:

    allow(obj).to receive(:message).and_return(value)

Cheers!



--
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/CAKxHnE1pcqwjoFukaqX9BymAyF_xGhm_GiUeOgk6DnqMg1zHqA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Arup Rakshit

unread,
Jul 3, 2014, 2:00:39 PM7/3/14
to rs...@googlegroups.com


On Thursday, July 3, 2014 9:49:56 PM UTC+5:30, Aaron Kromer wrote:

Arup,

Can you post your spec and exception. These should work on 2.14. If you are on RSpec 3, you need to either switch to the new syntax (https://www.relishapp.com/rspec/rspec-mocks/v/3-0/docs/basics/allowing-messages) or configure the old syntax (https://www.relishapp.com/rspec/rspec-mocks/v/3-0/docs/old-syntax).

Here is a sample with the new syntax:

    allow(obj).to receive(:message).and_return(value)

Cheers


Code :-

#!/usr/bin/env ruby

require 'ostruct'

class Foo
  def intialize
  #..
  end

  def meth
    OpenStruct.new(suml: sum, mul: multiplication)
  end

  private

  def sum(enum)
    enum.inject(:+)
  end

  def multiplication(enum)
    enum.inject(:*) 
  end
end

Rspec :

require_relative "../test.rb"

describe Foo do
  before do
    subject.should_receive(:sum).and_return(12)
    subject.should_receive(:multiplication)
  end

  describe "#meth" do
    it "return an OpenStruct instance" do
      expect(subject.meth).to be_instance_of(OpenStruct)
    end
  end
end


And finally -

arup@linux-wzza:~/Ruby> rspec  spec/test_spec.rb
.

Deprecation Warnings:

Using `should_receive` from rspec-mocks' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead. Called from /home/arup/Ruby/spec/test_spec.rb:5:in `block (2 levels) in <top (required)>'.


If you need more of the backtrace for any of these deprecations to
identify where to make the necessary changes, you can configure
`config.raise_errors_for_deprecations!`, and it will turn the
deprecation warnings into errors, giving you the full backtrace.

1 deprecation warning total

Finished in 0.00175 seconds (files took 0.13547 seconds to load)
1 example, 0 failures
 

Aaron Kromer

unread,
Jul 3, 2014, 3:51:25 PM7/3/14
to rs...@googlegroups.com

That’s stating your spec passed. Meaning it recognized your stub syntax. However, you have not explicitly configured the “should” syntax. RSpec is warning you that you should take action to correct this. The message is giving you two options:



--
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.

Arup Rakshit

unread,
Jul 4, 2014, 10:52:32 AM7/4/14
to rs...@googlegroups.com

> > 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/64c0b722-7585-4903-8926-5a4d350a55
> > b3%40googlegroups.com
> > <https://groups.google.com/d/msgid/rspec/64c0b722-7585-4903-8926-5a4d350a
> > 55b3%40googlegroups.com?utm_medium=email&utm_source=footer> .
> >
> > For more options, visit https://groups.google.com/d/optout.

Yes `allow` is working correctly.

require_relative "../test.rb"

describe Foo do
before do
allow(subject).to receive(:sum).and_return(12)
allow(subject).to receive(:multiplication).and_return(1)
end

describe "#meth" do
it "return an OpenStruct instance" do
expect(subject.meth).to be_instance_of(OpenStruct)
end
end
end

The above code is working. Thanks for your help.

--
================
Regards,
Arup Rakshit
================
Debugging is twice as hard as writing the code in the first place. Therefore,
if you write the code as cleverly as possible, you are, by definition, not
smart enough to debug it.

--Brian Kernighan

Arup Rakshit

unread,
Jul 4, 2014, 3:03:49 PM7/4/14
to rs...@googlegroups.com
On Thursday, July 03, 2014 03:51:24 PM Aaron Kromer wrote:
> That's stating your spec passed. Meaning it recognized your stub syntax.
> However, you have not explicitly configured the "should" syntax. RSpec is
> warning you that you should take action to correct this. The message is
> giving you two options:
>

From the http://stackoverflow.com/questions/14987141/rspec-stub-private-method

I found -

@foo.should_receive(:start_training).exactly(2).times

From relish doc I found

expect(obj).to respond_to(:foo).with(1).argument

Where those *times*, *exactly*, *argument*, *arguments* methods are
documented.. I am not finding it... I want to see, what else such varieties are
present ?

Arup Rakshit

unread,
Jul 4, 2014, 3:09:52 PM7/4/14
to rs...@googlegroups.com

> From the
> http://stackoverflow.com/questions/14987141/rspec-stub-private-method
>
> I found -
>
> @foo.should_receive(:start_training).exactly(2).times
>
> From relish doc I found
>
> expect(obj).to respond_to(:foo).with(1).argument
>
> Where those *times*, *exactly*, *argument*, *arguments* methods are
> documented.. I am not finding it... I want to see, what else such varieties
> are present ?

Just found it http://rubydoc.info/gems/rspec-expectations/RSpec/Matchers/BuiltIn/RespondTo#argument-instance_method

and

http://rubydoc.info/gems/rspec-expectations/RSpec/Matchers/BuiltIn/YieldControl#exactly-instance_method

:)
Reply all
Reply to author
Forward
0 new messages