I am learning to create simple Unit tests to eventually (and hopefully) start to do only TDD; for now I am trying to write tests for code already written to see what might cause problems. This is one of them.
Let's say I have this simple class (with Typescript-> Javascript):
class PrivateStuff {
greeting: string;
private _thisIsPrivate;
constructor(isPrivate: boolean) {
this._thisIsPrivate = isPrivate;
}
setPrivate(option) {
this._thisIsPrivate = option;
console.log("_thisIsPrivate changed to : " + option);
}
getPrivate() {
console.log("_thisIsPrivate is : " + this._thisIsPrivate);
return this._thisIsPrivate;
}
}
And I use it in this way:
let privateStuff = new PrivateStuff(false);
let buttonSet = document.createElement('button');
buttonSet.textContent = "Set True";
buttonSet.onclick = function () {
privateStuff.setPrivate(true);
}
let buttonGet = document.createElement('button');
buttonGet.textContent = "Get";
buttonGet.onclick = function() {
console.log(privateStuff.getPrivate());
}
document.body.appendChild(buttonSet);
document.body.appendChild(buttonGet);
setPrivate()
does not need to return anything, but because of that I cannot test it. When creating a unit test for it, should I refactor the code?
If I were doing TDD, should I always create methods that return something just to be able to test it? Or I am missing something?
P.S. You can see and run the code here