How to test accesors and mutators?

This is a discussion on How to test accesors and mutators? within the A Brief History of Cprogramming.com forums, part of the Community Boards category; It's kind of interesting that I've had a hard time testing my classes to make sure its mutator and accesor ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    19

    How to test accesors and mutators?

    It's kind of interesting that I've had a hard time testing my classes to make sure its mutator and accesor methods work, quite simply because testing one requires the other and viceversa. I know that in test-driven development it is usually adviced to not test them because they are simple enough to be "trusted". However, when a member of my function is an array I feel uneasy about not testing the methods that are supposed to access and mutate it, since an array is no longer as simple as a variable. Then again my anxiety might just be typical in a newbie like me. I wonder though, if there is actually any method to test mutators, even if they are so intricately intertwined. They are quite simply supposed to be the only passageways of data in and out of the object. Should I just not test them? I definitely have always left out the possibility of making member variables public, even if just momentarily to test a mutator (for example) without having to use the accesor. I think it's not elegant, and not recomendable for continuous testing.

    By the way I am programming in Visual Basic 6.0, something which I still find somewhat shameful, particularly because trying to do Object Oriented Programming is complicated

    Thanks for the input!
    Last edited by jalnewbie; 05-02-2007 at 09:33 PM. Reason: I forgot to mention which lenguage I'm working with

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Well, one thing you can do is test the invariant of accessors:
    setValue(v) -> getValue() == v if v is a valid value.
    Of course, if you do some index calculation which just doesn't work, and copy&pasted it from one to the other, that's harder to detect.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21