在如何处理此测试套件方面存在问题。我被要求在Bartender类的方法中实例化Beer类。我希望我的takeOrder方法接受多个输入,因此我需要在takeOrder中调用Beer类构造函数。
这是我的啤酒类:
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;
}
}我必须将一个Beer实例推入Bartender类的orders数组中,但我不确定如何处理。下面是我的Bartender类的代码:
class Bartender {
constructor(name, hourlyWage){
this.name = name;
this.hourlyWage = hourlyWage;
this.orders = [];
}
takeOrder(newOrder) {
var newOrder = new Beer();
this.orders.push(newOrder);
}
}在我的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)这是我不断失败的调酒师单元测试:
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如果有人有任何建议。谢谢。
发布于 2021-09-23 16:38:03
你的代码有两个问题。
对于First,bartender.takeOrder函数有一个错误。没有使用newOrder作为参数,而是重新声明了它。建议修复
takeOrder(newOrder) {
this.order.push(new Beer(newOrder))Second,您向bartender.takeOrder传递了错误的参数。运行时,只使用了第一个参数"Grand Teton Brewing",而丢弃了rest。
bartender.takeOrder("Grand Teton Brewing", "Bitch Creek", "Brown Ale", 7, 16);建议修复,将它们包装在一个对象中:
bartender.takeOrder({
brewer: 'Grand Teton Brewing',
name: '',
...
})https://stackoverflow.com/questions/69303704
复制相似问题