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.
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: '',
...
})