0

我尝试做一个小的重构,但它破坏了所有测试。

我有很多如果我想通过使用跳表来摆脱。

我想从这里出发:


export class Pharmacy {
  constructor(drugs = []) {
    this.drugs = drugs
  }

  updatePharmacyBenefits() {
    this.drugs.forEach((drug) => {
      if (drug.name !== 'Magic Pill')
        drug.setBenefitStrategy(new MagicPillStrategy())
      if (drug.name === 'Herbal Tea')
        drug.setBenefitStrategy(new HerbalTeaStrategy())
      if (drug.name === 'Fervex')
        drug.setBenefitStrategy(new FervexStrategy())
      if (drug.name === 'Dafalgan')
        drug.setBenefitStrategy(new DafalganStrategy())

      drug.updateBenefit()
      drug.updateExpiration()
    })

    return this.drugs
  }
}

到这里 :

export class Pharmacy {
  constructor(drugs = []) {
    this.drugs = drugs
  }

  updatePharmacyBenefits() {
    const drugStrategies = {
      'Herbal Tea': new HerbalTeaStrategy(),
      'Magic Pill': new MagicPillStrategy(),
      Fervex: new FervexStrategy(),
      Dafalgan: new DafalganStrategy(),
    }
    const specialsDrugs = ['Herbal Tea', 'Fervex', 'Magic Pill', 'Dafalgan']

    this.drugs.forEach((drug) => {
      if (drug.name.includes(specialsDrugs))
        drug.setBenefitStrategy(drugStrategies[drug.name])

      drug.updateBenefit()
      drug.updateExpiration()
    })

    return this.drugs
  }
}

我没有任何有意义的错误消息,如果什么都没发生,我可以观察到我所有的测试都失败了。

4

0 回答 0