如何在JS单元测试中模拟静态变量?

问题描述 投票:0回答:1

在业障覆盖率测试中,我获得了99.3%的覆盖率。为了使其达到100%,我需要测试此功能中else部分的帮助:

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});
  }
}

一个小提示是在测试中模拟/覆盖CURRENCY_UNITS。您可能要使用下面的函数/变量:

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));}

这是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'
}};

这是我尝试测试的代码,但仍然无法正常工作:

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';
});
javascript typescript junit karma-jasmine web-frontend
1个回答
0
投票

对于那些想知道我刚刚想出答案的人,您可以在这里看到:

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());
});
© www.soinside.com 2019 - 2024. All rights reserved.