对如何处理这个测试套件有问题。我被要求在Bartender类的方法中实例化Beer类。我想让我的takeOrder方法接受多个输入,因此我需要在takeOrder中调用Beer类构造函数。
这是我的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);
});
如果有人有什么建议的话,我想听听。谢谢。你的代码有两个问题。
首先,bartender.takeOrder
函数有一个错误。没有使用newOrder
作为参数,而是重新声明了它。建议修复
takeOrder(newOrder) {
this.order.push(new Beer(newOrder))
第二,你传递了错误的参数给bartender.takeOrder
。当您运行时,只使用第一个参数"Grand Teton Brewing"
,其余的被丢弃。
bartender.takeOrder("Grand Teton Brewing", "Bitch Creek", "Brown Ale", 7, 16);
建议修复,将它们包装在一个对象中:
bartender.takeOrder({
brewer: 'Grand Teton Brewing',
name: '',
...
})