How to hide private methods from included Helper module

15 views
Skip to first unread message

Evan Brodie

unread,
May 6, 2017, 5:43:42 PM5/6/17
to rspec
Hello,

In the application that I am working on, the rspec configuration includes several modules that are "mixed-in" to rspec via config.include, in an RSpec.configuration block. This would make all the instance methods of that module available to my test classes. For example, I have the following helper to abstract some of the login functionality in my acceptance tests:

module LoginHelper
 
def login(user)
   
# Insert login logic here
 
end
end


The implementation of the login method has begun to grow and is starting to include some duplicated code, so I naturally want to abstract some of this duplication out into private methods, to ease code readability. However, it appears that any private method included into my rspec configuration from these "mixed-in" modules will also be visible to my test code. I can verify this by placing a specific raise message into my private method and witness that specific error message appear when the test calls this method and ultimately fails. What I perhaps hoped was that the test would instead fail due to a method scope issue.

I am seeking a way to have methods from these "mixed-in" modules be hidden from test code and only available to public methods from within its module, so that I do not risk having these private methods get accidentally called by the test code. Is there a way to accomplish this in the current version of rspec?

Thank you.
Evan

Myron Marston

unread,
May 6, 2017, 5:46:33 PM5/6/17
to rs...@googlegroups.com
RSpec isn't doing anything special to create the behavior you're describing, and there's nothing we can do to prevent it. It's simply how Ruby modules and private methods work.  Private methods are can be called as long as the implicit `self` receiver -- which means a private method from a superclass can be called from a subclass.  Module inclusion puts the module into the ancestor chain of the including class, effectively making the module a superclass, allowing private methods in the module to be called from the including class.

HTH,
Myron

--
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+unsubscribe@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/c4cebf6d-b5a8-4c20-b2e4-7aa62824c3f5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages