# Specing protected methods

**URL:** <https://www.ruby-forum.com/t/specing-protected-methods/151596>\
**Category:** RSpec\
**Created:** [October 23, 2008, 11:19am UTC](https://www.ruby-forum.com/t/specing-protected-methods/151596 "2008-10-23T11:19:39Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![bastien-vaucher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@bastien-vaucher](https://www.ruby-forum.com/u/bastien-vaucher)\
**Post date:** [October 23, 2008, 11:19am UTC](https://www.ruby-forum.com/t/specing-protected-methods/151596/1 "2008-10-23T11:19:39Z")

</div>

Hi everyone,  
Do anyone know if there’s a way to spec protected methods ?  
Thanks in advance

– Bastien

---

<div class="post-metadata">

**Author:** ![bastien-vaucher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@bastien-vaucher](https://www.ruby-forum.com/u/bastien-vaucher)\
**Post date:** [October 23, 2008, 12:40pm UTC](https://www.ruby-forum.com/t/specing-protected-methods/151596/2 "2008-10-23T12:40:42Z")

</div>

On 23 Oct 2008, at 10:16, Bastien wrote:

> Hi everyone,  
> Do anyone know if there’s a way to spec protected methods ?  
> Thanks in advance

Sorry if this sounds preachy, but I would always advise you to factor  
out the code in the method into another class, then test that class.

If the behaviour of the method is so complex you need to test it on  
it’s own, it deserves to be in a class all by itself.

This is what we call, ‘listening to your specs’ - if it’s hard to  
spec, you’ve probably got the design wrong.

HTH,  
Matt

---

<div class="post-metadata">

**Author:** ![bastien-vaucher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@bastien-vaucher](https://www.ruby-forum.com/u/bastien-vaucher)\
**Post date:** [October 23, 2008, 1:51pm UTC](https://www.ruby-forum.com/t/specing-protected-methods/151596/3 "2008-10-23T13:51:46Z")

</div>

I totally agree with the ''listening to your specs" concepts and  
always divide my code into small methods easier to spec.  
Now let me get you right, here’s my code :

class Survey \< ActiveRecord::Base  
has\_many :participants  
…  
def generate\_reports  
…  
sub\_total = sub\_total(participants.reports)  
…  
end

protected

def sub\_total(reports)  
sub\_total = 0  
reports.collect do |report|  
sub\_total = sub\_total + report.score  
end  
return sub\_total  
end

end

I m already specing the “generate\_reports” methods with a mock on  
“sub\_total”, but I would also like to spec that method. I’ve put this  
method into protected because it doesn’t make sens to call it from  
outside this class. But you’re arguing that I should put that method  
into a lib ? I m not sure it would make sense as this method is very  
specific to that class, and would actually be easy to spec. Except  
that it’s not possible to call a protected method from a spec as far  
as I know, correct me if I m wrong. (I maybe didn’t explain my problem  
well in the first place)

Regards

– Bastien

---

<div class="post-metadata">

**Author:** ![bastien-vaucher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@bastien-vaucher](https://www.ruby-forum.com/u/bastien-vaucher)\
**Post date:** [October 23, 2008, 3:53pm UTC](https://www.ruby-forum.com/t/specing-protected-methods/151596/4 "2008-10-23T15:53:01Z")

</div>

Bastien [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> ```
> ...
> 
> ```
> 
> end
> 
> end
> 
> I m already specing the “generate\_reports” methods with a mock on  
> “sub\_total”

Why are you mocking #sub\_total?

Pat

---

<div class="post-metadata">

**Author:** ![bastien-vaucher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@bastien-vaucher](https://www.ruby-forum.com/u/bastien-vaucher)\
**Post date:** [October 23, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/specing-protected-methods/151596/5 "2008-10-23T15:54:12Z")

</div>

On Thu, Oct 23, 2008 at 6:49 AM, Bastien [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> …  
> end  
> as I know, correct me if I m wrong. (I maybe didn’t explain my problem  
> well in the first place)

This sort of decision is always a balancing act. Leaving it where it  
is means you have to either

- unprotect it, which you clearly prefer not to do
- expose it for you code example using clever ruby trickery
- give up testing it directly
- move it to another class, adding conceptual weight to your system

None of these are perfect and all come with some tradeoff.

I’ll confess that, in practice, I’d probably not test this directly.  
More often than not, for me, the sub\_total method is a result of  
refactoring the generate\_reports method, which by that time would have  
at least two code examples in which the only difference is the sum of  
scores of the reports.

In this case, however, assuming participants and reports are AR backed  
models, you don’t really need to introduce a new class:

subtotal = participants.reports.sum(‘score’)

Cheers,  
David

---

<div class="post-metadata">

**Author:** ![bastien-vaucher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@bastien-vaucher](https://www.ruby-forum.com/u/bastien-vaucher)\
**Post date:** [October 23, 2008, 5:22pm UTC](https://www.ruby-forum.com/t/specing-protected-methods/151596/6 "2008-10-23T17:22:13Z")

</div>

Thanks for all your answers.

I was just wondering how about using send in my specs ? It would  
bypass the protected limitation without altering my code :

@survey.send( :sub\_total, [@mock\_report] )

Is that the kind of trickery you were referring to David ? Would that  
be bad practice ?

Regards

– Bastien

---

<div class="post-metadata">

**Author:** ![bastien-vaucher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@bastien-vaucher](https://www.ruby-forum.com/u/bastien-vaucher)\
**Post date:** [October 23, 2008, 3:38pm UTC](https://www.ruby-forum.com/t/specing-protected-methods/151596/7 "2008-10-23T15:38:57Z")

</div>

On Thu, Oct 23, 2008 at 7:49 AM, Bastien [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I m already specing the “generate\_reports” methods with a mock on  
> “sub\_total”, but I would also like to spec that method. I’ve put this  
> method into protected because it doesn’t make sens to call it from  
> outside this class. But you’re arguing that I should put that method  
> into a lib ? I m not sure it would make sense as this method is very  
> specific to that class, and would actually be easy to spec. Except  
> that it’s not possible to call a protected method from a spec as far  
> as I know, correct me if I m wrong. (I maybe didn’t explain my problem  
> well in the first place)

If you’re somehow mocking the sub\_total method while specing the  
generate\_reports method, don’t do that. Don’t stub/mock the thing that  
you’re specing, only stub/mock its collaborators, i.e., its  
dependencies. I  
would either verify via generate\_reports that the sub totals on the  
report  
are correct, or I’d factor out something like a ReportGenerator class  
and  
spec it. Then, I’d stub/mock out the ReportGenerator in the specs for  
the  
Survey class.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![bastien-vaucher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@bastien-vaucher](https://www.ruby-forum.com/u/bastien-vaucher)\
**Post date:** [October 23, 2008, 5:40pm UTC](https://www.ruby-forum.com/t/specing-protected-methods/151596/8 "2008-10-23T17:40:58Z")

</div>

On Thu, Oct 23, 2008 at 10:00 AM, Bastien [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thanks for all your answers.
> 
> I was just wondering how about using send in my specs ? It would  
> bypass the protected limitation without altering my code :
> 
> @survey.send( :sub\_total, [@mock\_report] )
> 
> Is that the kind of trickery you were referring to David ? Would that  
> be bad practice ?

If you’re going to do this, use **send** or your code examples will  
break if/when you go to Ruby \>= 1.9.

As for good/bad practice, here’s the way I see it: there is a lot of  
evidence that encapsulation is a good thing in that it constrains  
contact points between objects, making them less coupled and easier to  
modify internally, swap in and out (polymorphism), etc, etc.

Every time you violate encapsulation by using **send** , even in code  
examples, you increase coupling by a little bit, and make your system  
that much more brittle. So while one violation might seem harmless,  
it’s a slippery slope (sorry for the cliché).

With all that - you’ll find **send** in my code, and in my code  
examples. But I’m conscious of the risks. FWIW.

Cheers,  
David

---

<div class="post-metadata">

**Author:** ![bastien-vaucher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@bastien-vaucher](https://www.ruby-forum.com/u/bastien-vaucher)\
**Post date:** [October 23, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/specing-protected-methods/151596/9 "2008-10-23T21:01:47Z")

</div>

It appears we were too clever, but I think this would be a reasonable  
use of stories.

We tried to have a story file run with 2 different sets of steps to  
use the same story against both the UI using selenium and against the  
server API using regular get/put/post. This way we could spec the  
server API and the UI while ensuring they are in sync for the scenario  
in question. But stories get confused when there are stories or  
scenarios with the same names.

Does Cucumber deal with this any better than the story runner? Given  
that it searches for steps rather than having them declared, I would  
think you can not share story files for different steps?

Michael

---

<div class="post-metadata">

**Author:** ![bastien-vaucher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@bastien-vaucher](https://www.ruby-forum.com/u/bastien-vaucher)\
**Post date:** [October 23, 2008, 10:57pm UTC](https://www.ruby-forum.com/t/specing-protected-methods/151596/10 "2008-10-23T22:57:17Z")

</div>

On Thu, Oct 23, 2008 at 1:20 PM, Michael L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Does Cucumber deal with this any better than the story runner? Given that  
> it searches for steps rather than having them declared, I would think you  
> can not share story files for different steps?

You can do this, you’d just put the steps in different directories and  
tell cucumber to require the files in one directory for one run, and  
another for another run. ie:

cucumber --require features/steps/selenium/ features/  
AND  
cucumber --require features/steps/normal/ features/

–  
Zach D.  
[http://www.continuousthinking.com](http://www.continuousthinking.com)

> **[Homepage | Mutually Human](https://www.mutuallyhuman.com)**
>
> We empower businesses to overcome challenges with a holistic focus on People, Process, and Technology for lasting results and innovation.

---

<div class="post-metadata">

**Author:** ![bastien-vaucher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@bastien-vaucher](https://www.ruby-forum.com/u/bastien-vaucher)\
**Post date:** [October 23, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/specing-protected-methods/151596/11 "2008-10-23T23:03:09Z")

</div>

On Thu, Oct 23, 2008 at 5:16 AM, Bastien [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi everyone,  
> Do anyone know if there’s a way to spec protected methods ?  
> Thanks in advance

FWIW, I blogged my answer to this question recently:  
[http://avdi.org/devblog/2008/10/21/testing-private-methods/](http://avdi.org/devblog/2008/10/21/testing-private-methods/)

–  
Avdi

Home: [http://avdi.org](http://avdi.org)  
Developer Blog: [avdi.codes – Avdi Grimm, Code Cleric](http://avdi.org/devblog/)  
Twitter: [http://twitter.com/avdi](http://twitter.com/avdi)  
Journal: [http://avdi.livejournal.com](http://avdi.livejournal.com)

---

<div class="post-metadata">

**Author:** ![bastien-vaucher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@bastien-vaucher](https://www.ruby-forum.com/u/bastien-vaucher)\
**Post date:** [October 23, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/specing-protected-methods/151596/12 "2008-10-23T23:11:57Z")

</div>

On Thu, Oct 23, 2008 at 5:00 PM, Avdi G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> FWIW, I blogged my answer to this question recently:  
> [http://avdi.org/devblog/2008/10/21/testing-private-methods/](http://avdi.org/devblog/2008/10/21/testing-private-methods/)

…and to be a little more concrete, at first glance the way I’d apply  
that advice to your code example is to make a class that either  
subclasses or wraps (via DelegateClass/SimpleDelegate) Array and adds  
a #sub\_total method, something like the following:

class Survey \< ActiveRecord::Base  
has\_many :participants  
…  
def generate\_reports  
…  
sub\_total = ReportCollection.new(participants.reports).sub\_total  
…  
end

class ReportCollection \< SimpleDelegator  
def sub\_total  
sub\_total = 0  
self.collect do |report|  
sub\_total = sub\_total + report.score  
end  
return sub\_total  
end  
end

end

–  
Avdi

Home: [http://avdi.org](http://avdi.org)  
Developer Blog: [avdi.codes – Avdi Grimm, Code Cleric](http://avdi.org/devblog/)  
Twitter: [http://twitter.com/avdi](http://twitter.com/avdi)  
Journal: [http://avdi.livejournal.com](http://avdi.livejournal.com)

---

<div class="post-metadata">

**Author:** ![bastien-vaucher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@bastien-vaucher](https://www.ruby-forum.com/u/bastien-vaucher)\
**Post date:** [October 23, 2008, 11:22pm UTC](https://www.ruby-forum.com/t/specing-protected-methods/151596/13 "2008-10-23T23:22:29Z")

</div>

That works when running one at a time. When I want to run all my  
specs how do I ensure the requires are applied to the appropriate  
features? It is not all my stories, only a subset that are dual-  
purposed. Do I need to have directories for those that can be run  
dual mode and those that are not, and then have a script that runs the  
3 cases?

Michael

---

<div class="post-metadata">

**Author:** ![bastien-vaucher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@bastien-vaucher](https://www.ruby-forum.com/u/bastien-vaucher)\
**Post date:** [October 23, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/specing-protected-methods/151596/14 "2008-10-23T23:55:54Z")

</div>

On Thu, Oct 23, 2008 at 11:20 PM, Michael L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That works when running one at a time. When I want to run all my specs how  
> do I ensure the requires are applied to the appropriate features? It is not  
> all my stories, only a subset that are dual-purposed. Do I need to have  
> directories for those that can be run dual mode and those that are not, and  
> then have a script that runs the 3 cases?

You have to run once for each “environment”. So if you have say  
selenium and rails, you’ll have to run all features that use selenium  
in one run and those that use rails in another. (Of course you can  
have overlap - some features that include run in both runs)

Aslak

---

<div class="post-metadata">

**Author:** ![bastien-vaucher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed655f/32.png) [@bastien-vaucher](https://www.ruby-forum.com/u/bastien-vaucher)\
**Post date:** [October 24, 2008, 7:58am UTC](https://www.ruby-forum.com/t/specing-protected-methods/151596/15 "2008-10-24T07:58:53Z")

</div>

On Thu, Oct 23, 2008 at 7:20 PM, Michael L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Does Cucumber deal with this any better than the story runner? Given that  
> it searches for steps rather than having them declared, I would think you  
> can not share story files for different steps?

I’ve done this on a couple of projects.

steps/common/_\_steps.rb  
steps/watir/_\_steps.rb  
steps/rails/\*\_steps.rb

Set up two tasks and use --require. Or use --profile.

See

> **[GitHub · Change is constant. GitHub keeps you ahead. · GitHub](https://github.com/)**
>
> Join the world's most widely adopted, AI-powered developer platform where millions of developers, businesses, and the largest open source community build software that advances humanity.

for details.

Aslak
