RSpec: Stub private method

23tux picture 23tux · Feb 20, 2013 · Viewed 35.2k times · Source

I try to test a class with RSpec2, that has some private methods, which are called from some public methods. I test the public methods with

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

if they are called and how often. My problem is, that this approach doesn't work with private methods. So, is there any way to use sth like @foo.send(:private_method) in combination with should_receive? Or any other syntax?

Answer

Justin Aiken picture Justin Aiken · Feb 20, 2013

should_receive(:method) works whether the visibility of :method is public or private.