Let's say you have some class like
class Foo
...
public def methodA
x = methodB(true)
# other operations (assume x is not the return value of methodA)
end
private def methodB(arg)
if arg
return 1
else
return 0
end
end
end
When you're writing a unit test for methodA, you want to check that x was assigned the right value, which means you have to check that the call to methodB returned 1 like you expected.
How would you test that in rspec?
Right now I have (with help from How to test if method is called in RSpec but do not override the return value)
@foo = Foo.new
expect(@foo).to receive(:methodB).with(true).and_call_original
But I don't know how to verify the actual return value of methodB.
I think instead of mocking you can just call the private method?
This is basically testing the private method directly. Some people frown upon doing that but if it has lots of logic it's sometimes easier to test it directly. I agree with @spickermann that it's usually best to test the public method and leave the implementation details of the private methods out of the specs.