lundi 23 avril 2018

Having trouble testing class private property accessor in Typescript

I have written the test code for the getter setter of class.

Even though I have not actually write a getter setter yet. Test passed.

I am curious about...

  • Wrong test code?
  • Can I test getter and setter behavior?
  • Is there a better way to test getters and setters?

// Code
class PersonalInfo {
  public name: string = 'kevin';
  private _ssn: string = '123-45-XXXX';

  // I will write getter, setter for _ssn like below. Now now.

  // get ssn(): string {
  //   return this._ssn;
  // }

  // set ssn(newSSN: string) {
  //   this._ssn = newSSN;
  // }
}

// TestCode
describe('Testing getter, setter', () => {
  it('Test whether getter and setter are working correctly', () => {
    const personalInfo = new PersonalInfo();
    personalInfo.ssn = 'XXX-12-3456' // set ssn

    expect(personalInfo.ssn).toBe('XXX-12-3456'); // get ssn
  })
})


Test passed even though no getter setter was written

Testing getter, setter
  ✓ Test whether getter and setter are working correctly (1ms)

Thank you for your help!

Aucun commentaire:

Enregistrer un commentaire