简体   繁体   中英

How can I mock a static variable in JS unit test?

In the karma coverage test, I got 99.3% coverage. In order to make it 100%, I need help for testing the else part in this function:

createCurrencyUnits(): void {
var keys = Object.keys(ObjectsDomainConstants.CURRENCY_UNITS);
for (var i = 0; i < keys.length; i++) {
  if (ObjectsDomainConstants.CURRENCY_UNITS[keys[i]].base_unit === null) {
    // Base unit (like: rupees, dollar etc.).
    createUnit(ObjectsDomainConstants.CURRENCY_UNITS[keys[i]].name, {
      aliases: ObjectsDomainConstants.CURRENCY_UNITS[keys[i]].aliases});
  } else {
    // Sub unit (like: paise, cents etc.).
    createUnit(ObjectsDomainConstants.CURRENCY_UNITS[keys[i]].name, { //<--red line at here
      definition: ObjectsDomainConstants.CURRENCY_UNITS[keys[i]].base_unit,
      aliases: ObjectsDomainConstants.CURRENCY_UNITS[keys[i]].aliases});
  }
}

A small hint is to mock/override the CURRENCY_UNITS in the test. You may want to use the function/variable below:

fromRawInputString(units: any): Units {
  try {
    this.createCurrencyUnits(); //<-- you can see this function call createCurrencyUnits()
  } catch (parsingError) {}
var compatibleUnits = this.toMathjsCompatibleString(units);
if (compatibleUnits !== '') {
  try {
    unit(compatibleUnits);
  } catch (err) {
    throw new Error(err);
  }
}
return new Units(this.fromStringToList(units));}

And this is the CURRENCY_UNITS:

public static CURRENCY_UNITS = {
dollar: {
  name: 'dollar',
  aliases: ['$', 'dollars', 'Dollars', 'Dollar', 'USD'],
  front_units: ['$'],
  base_unit: null
},
rupee: {
  name: 'rupee',
  aliases: ['Rs', 'rupees', '₹', 'Rupees', 'Rupee'],
  front_units: ['Rs ', '₹'],
  base_unit: null
},
cent: {
  name: 'cent',
  aliases: ['cents', 'Cents', 'Cent'],
  front_units: [],
  base_unit: '0.01 dollar'
},
paise: {
  name: 'paise',
  aliases: ['paisa', 'Paise', 'Paisa'],
  front_units: [],
  base_unit: '0.01 rupee'
}};

And here is the code that I tried to test, but still doesn't work:

it('should test the CURRENCY_UNITS', () => {
  ObjectsDomainConstants.CURRENCY_UNITS.cent.base_unit = '0.03 dollar';
  ObjectsDomainConstants.CURRENCY_UNITS.paise.base_unit = '0.02 rupee';
  expect(units.fromRawInputString('cent').toDict()).toEqual(
    new Units([{exponent: 1, unit: 'cent'}]).toDict());
  expect(units.fromRawInputString('paise').toDict()).toEqual(
    new Units([{exponent: 1, unit: 'paise'}]).toDict());
  ObjectsDomainConstants.CURRENCY_UNITS.cent.base_unit = '0.01 dollar';
  ObjectsDomainConstants.CURRENCY_UNITS.paise.base_unit = '0.01 rupee';
});

for those who want to know the answer that I just figure out, you can see here:

it('should test the CURRENCY_UNITS if the base unit is not null', () => {
  ObjectsDomainConstants.CURRENCY_UNITS.dollar.base_unit = '100 cent';
  expect(units.fromRawInputString('dollar').toDict()).toEqual(
    new Units([{exponent: 1, unit: 'dollar'}]).toDict());
});

it('should test the CURRENCY_UNITS if the base unit is null', () => {
  ObjectsDomainConstants.CURRENCY_UNITS.dollar.base_unit = null;
  expect(units.fromRawInputString('dollar').toDict()).toEqual(
    new Units([{exponent: 1, unit: 'dollar'}]).toDict());
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM