Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda