Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I only be testing public interfaces in BDD? (in general, and specifically in Ruby)

Tags:

tdd

rspec

bdd

I'm reading through the (still beta) rspec book by the prag progs as I'm interested in behavioral testing on objects. From what I've gleaned so far (caveat: after only reading for 30 min), the basic idea is that I want ensure my object behaves as expected 'externally' i.e. in its output and in relation to other objects.

Is it true then that I should just be black box testing my object to ensure the proper output/interaction with other objects?

This may be completely wrong, but given all of the focus on how my object behaves in the system, it seems this is ideology one would take. If that's so, how do we focus on the implementation of an object? How do I test that my private method is doing what I want it to do for all different types of input?

I suppose this question is maybe valid for all types of testing?? I'm still fairly new to TDD and BDD.

like image 262
brad Avatar asked Sep 03 '10 12:09

brad


1 Answers

If you want to understand BDD better, try thinking about it without using the word "test".

Instead of writing a test, you're going to write an example of how you can use your class (and you can't use it except through public methods). You're going to show why your class is valuable to other classes. You're defining the scope of your class's responsibilities, while showing (through mocks) what responsibilities are delegated elsewhere.

At the same time, you can question whether the responsibilities are appropriate, and tune the methods on your class to be as intuitively usable as possible. You're looking for code which is easy to understand and use, rather than code which is easy to write.

If you can think in terms of examples and providing value through behaviour, you'll create code that's easy to use, with examples and descriptions that other people can follow. You'll make your code safe and easy to change. If you think about testing, you'll pin it down so that nobody can break it. You'll make it hard to change.

If it's complex enough that there are internal methods you really want to test separately, break them out into another class then show why that class is valuable and what it does for the class that uses it.

Hope this helps!

like image 82
Lunivore Avatar answered Oct 09 '22 17:10

Lunivore