Unexpected test failure

Now isn't it very interesting that one of our tests actually failed; TrackModel Creation Verify mute FAILED and Expected 1 to be 5.. This failure was not preplanned for the book; it is actually a real corner case that we just found because we started to use unit testing. If you want to take a quick look at the code, here is the TrackModel.ts code stripped down to just show the relevant routines:

export class TrackModel implements ITrack { 
private _volume: number = 1;
private _mute: boolean;
private _origVolume: number;
constructor(model?: ITrack) {
if (model) {
for (let key in model) {
this[key] = model[key];
}
}
}

public set mute(value: boolean) {
value = typeof value === 'undefined' ? false : value;
this._mute = value;
if (this._mute) {
this._origVolume = this._volume;
this.volume = 0;
} else {
this.volume = this._origVolume;
}
}

public set volume(value: number) {
value = typeof value === 'undefined' ? 1 : value;
this._volume = value;
if (this._volume > 0 && this._mute) {
this._origVolume = this._volume;
this._mute = false;
}
}
}

Now, I'll give you a few minutes to look at the preceding test code and this code and check whether you can spot why the test fails.

Good, I see, you are back; did you see where the corner case is? Don't feel bad if you couldn't find it quickly; it took me a few minutes to figure out why it was failing also.

Well, first, look at the error message; it said Verify Mute FAILED, so this means it was our mute test that failed. Then, we put Verify mute in the it function that tested our mute functionality. The second clue is the error, Expected 1 to be 5. So, we expected something to be 5, but it was actually 1. So, this specific test and this line of code are failing in the test:

 it ('Verify mute', () => {
expect(trackModel.volume).toBe(5);
});
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.21.237