RSpec: Stub private method
should_receive(:method)
works whether the visibility of :method
is public or private.
You can use allow_any_instance_of
method to stub or mock any instance of a classfor e.g. you have a class
named Foo
with some private
methods than you can do something like this
allow_any_instance_of(Foo).to receive(:private_method) do #do somethingend
In case if there you have module
also, you can do something like this
allow_any_instance_of(Module::Foo).to receive(:private_method) do #do somethingend
You can find more details about allow_any_instance_of()
method at Official Documentation
Why do you want to test the private methods? They're private for a reason; to prevent access from external calls. Testing the public methods that rely on the private methods should be sufficient.