Topic: testing protected methods?

I searched for this and was surprised to not find anything. I didn't really even find anything on google either. Surely I'm not the only one to wonder what is the proper way to (unit) test protected (and private) methods?

For now I have merely made them public. But that's a poor solution, I really would like them to be protected and testable. Is this possible?

Re: testing protected methods?

Good question.

If you use "send" to call the method it will bypass this, but that's a bit of a hack.

@foo.send(:some_protected_method)

Even more of a hack, but it may be more convenient, is to make all the methods public in your test file:

# test file
class Foo
  public *protected_methods.collect(&:to_sym)
  public *private_methods.collect(&:to_sym)
end

I don't even know if this will work though, untested.

Just keep in mind, this may cause your test code to behave differently then the real code because you are actually changing something inside the class.

Railscasts - Free Ruby on Rails Screencasts