[rspec-users] State Based vs. Behaviour Based Spec/Testing

Pat Maddox pergesu at gmail.com
Sun Mar 25 02:25:13 EDT 2007

On 3/24/07, Scott Taylor <mailing_lists at railsnewbie.com> wrote:
> 1.  Should you test protected and private methods in your specs?

No.  You're specifying the behavior of an object as seen by the
outside world.  Of course protected and private methods aren't
available to the world, so why would you need to test them?  When
you're doing state-based testing, just call a method and then verify
that it returns the expected value or leaves your object in an
expected state.

> 2.  Should you ever test that a certain instance variable gets set in
> a method, or only that methods return their correct values?  This
> question can be generalized to: Should you ever test/spec *parts* of
> a method?

Same thinking behind my answer to (1) applies here.

> 3.  If you already have a code base (with no specs/tests) is there
> any advantage over using Rspec to Test::Unit for verification testing?


my_object.foo.should == 3
assert_equal 3, my_object.foo

my_object.should_not be_nil
assert !my_object.nil?

my_object.should have(2).errors
assert_equal 2, my_object.errors.size

Ultimately it comes down to personal preference.  If you're doing
things the right way, you're going to do basically the same thing in
RSpec or Test::Unit, just with a different syntax.  However if you're
like me, RSpec makes it easier to do things the right way (or at least
get close :)


More information about the rspec-users mailing list