Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
How to approach instantiating a class within a class in unit testing using JS

Having issues on how to approach this test suite. I am asked to instantiate the Beer class within a method in the Bartender class. I want my takeOrder method to take in multiple inputs so therefore I need to call the Beer class constructor inside takeOrder.

This is my Beer class:

  constructor(newBeer) {
    this.brewer = newBeer.brewer;
    this.name = newBeer.name;
    this.type = newBeer.type;
    this.price = newBeer.price;
    this.volume = newBeer.volume;
    this.isFlat = false;
  }
}

I have to push an instance of Beer into the orders array in the Bartender class and I am not sure how to approach. Here's my code for the Bartender class:

class Bartender {
  constructor(name, hourlyWage){
    this.name = name;
    this.hourlyWage = hourlyWage;
    this.orders = [];
  }
  takeOrder(newOrder) {
    var newOrder = new Beer();
    this.orders.push(newOrder);
  }
}

I keep getting this error message on my npm test:

 1) Bartender
       should be able to take orders:
     AssertionError: expected 'Grand Teton Brewing' to be an instance of Beer
      at Context.<anonymous> (test/bartender-test.js:33:12)
      at processImmediate (node:internal/timers:464:21)

This is the bartender unit test I keep failing:

it('should be able to take orders', function() {
    var bartender = new Bartender("Chaz", 8.50);

    bartender.takeOrder("Grand Teton Brewing", "Bitch Creek", "Brown Ale", 7, 16);

    assert.instanceOf(bartender.orders[0], Beer);
    assert.equal(bartender.orders.length, 1);
    assert.equal(bartender.orders[0].brewer, 'Grand Teton Brewing');
    assert.equal(bartender.orders[0].name, 'Bitch Creek');
    assert.equal(bartender.orders[0].type, 'Brown Ale');
    assert.equal(bartender.orders[0].price, 7);
    assert.equal(bartender.orders[0].volume, 16);
  });

Lmk if anyone has any advice. Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are two problems with your code.

First, the bartender.takeOrder function has a bug. newOrder as an argument was not used, rather, it was re-declared. Suggest fix

takeOrder(newOrder) {
  this.order.push(new Beer(newOrder))

Second, you passed the wrong argument to bartender.takeOrder. When you run, only the first argument "Grand Teton Brewing" was used, and rest was discarded.

bartender.takeOrder("Grand Teton Brewing", "Bitch Creek", "Brown Ale", 7, 16);

Suggest fix, wrap them in an object:

bartender.takeOrder({
  brewer: 'Grand Teton Brewing',
  name: '',
  ...
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda