I have a very common function that I have always unit tested in the same way, but I'm wondering if there is a better solution or if it's even possible a code smell is involved. It seems like a very simple case but I have a function that clears the properties of the object. Working in JavaScript, here is a simple example:
function Dog(name, owner) {
this.name = name;
this.owner = owner;
this.reset = function() {
this.name = '';
this.owner = '';
};
}
var puppy = new Dog('Max', 'Timmy');
console.log(puppy.name) // logs "Max"
puppy.reset();
console.log(puppy.name) // logs ""
I would normally unit test by setting the properties, calling the clear function, and then asserting that the properties were indeed set back to the defaults or "cleared out". The reason I'm asking about such a simple solution is because of the dogma that unit tests should only have 1 assertion. I also think that a "reset" type function could get way out of hand when it is dealing with a large number of properties (i.e. an object that is meant to store a SPA's state).
I'm sure that I am over-thinking this but wanted to get some outside opinion/criticism for something I have been doing the same for many years. I just cannot possibly think of a better way to do this.
Another question could be, are unit tests surrounding a reset function necessary? To me they seem to almost just test the language implementation -- similar to a getter/setter property.
...because of the dogma that unit tests should only have 1 assertion
- that's a bit over the top in my opinion. I'd say that unit test should - preferably - only have assertions that verify one type, or one aspect of behavior (and not just everything one could think of). It ensures that one bug doesn't hide other bugs. But it doesn't mean that there must be literally one assertion per test and no more. – Konrad Morawski Dec 23 '14 at 14:48reset()
function every time I add a new property to the object. I will continue doing this, but was hoping for some enlightening/revolutionary solution :) – adam-beck Dec 23 '14 at 14:56Assert
test data initial state. How else can you possibly provereset()
works if the initial state is not known/proven? FURTHER, in the sense that I'm setting "state", then there may be several objecs/object-properties to test for the proper state. – radarbob Dec 23 '14 at 15:26