Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
Cómo abordar la creación de instancias de una clase dentro de una clase en pruebas unitarias usando JS

Tener problemas sobre cómo abordar este conjunto de pruebas. Me piden que cree una instancia de la clase Beer dentro de un método en la clase Bartender. Quiero que mi método takeOrder admita múltiples entradas, por lo tanto, necesito llamar al constructor de la clase Beer dentro de takeOrder.

Esta es mi clase de cerveza:

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

Tengo que insertar una instancia de Beer en la matriz de pedidos en la clase Bartender y no estoy seguro de cómo acercarme. Aquí está mi código para la clase Bartender:

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

Sigo recibiendo este mensaje de error en mi prueba npm:

 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)

Esta es la prueba de unidad de camarero que sigo fallando:

 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 si alguien tiene algún consejo. Gracias.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Hay dos problemas con su código.

Primero , la función bartender.takeOrder tiene un error. newOrder no se usó como argumento, sino que se volvió a declarar. Sugerir arreglo

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

En segundo lugar , pasó el argumento incorrecto a bartender.takeOrder . Cuando ejecuta, solo se usó el primer argumento "Grand Teton Brewing" y el resto se descartó.

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

Sugerir arreglar, envolverlos en un objeto:

 bartender.takeOrder({ brewer: 'Grand Teton Brewing', name: '', ... })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!