gpt4 book ai didi

javascript - 测试未返回预期结果 - 售票员套路

转载 作者:行者123 更新时间:2023-11-28 21:22:15 25 4
gpt4 key购买 nike

我正在尝试通过一些套路来练习我的 javascript 测试。我不太明白为什么这不起作用,我设法用类似的销售功能很容易地做了一个 ruby​​ 版本。

ticketClark.js

var TicketClerk = function() {
this.till = { 25: 0, 50: 0, 100: 0 };
};

TicketClerk.prototype.sell = function(array) {
for (var i = 0; i < array.length; i++) {
if (this.canMakeChange(array[i])) {
this.giveChange(array[i]);
} else {
return "NO";
}
}
return "YES";
};

TicketClerk.prototype.canMakeChange = function(note) {
if (note === 50) {
return this.till[25] > 0;
}
if (note === 100) {
return this.canGiveFiftyTwentyFive() || this.canGiveThreeTwentyFives();
}
return true;
};

TicketClerk.prototype.giveChange = function(note) {
if (note === 25) {
this.till[25]++;
}
if (note === 50) {
this.till[25]--;
this.till[50]++;
}
if (note === 100 && this.canGiveFiftyTwentyFive()) {
this.till[25]--;
this.till[50]--;
this.till[100]++;
}
if (note === 100 && this.canGiveThreeTwentyFives()) {
this.till[25] -= 3;
this.till[100]++;
}
};

TicketClerk.prototype.canGiveThreeTwentyFives = function() {
return this.till[25] > 2;
};

TicketClerk.prototype.canGiveFiftyTwentyFive = function() {
return this.till[25] > 0 && this.till[50] > 0;
};

测试.js

describe("#TicketClerk", function() {
beforeEach(function() {
ticketClerk = new TicketClerk();
});
describe("#initialize", function() {
it("shows a hash of the money in the till, which starts with zero of each denominator", function() {
expect(ticketClerk.till).toEqual({ 25: 0, 50: 0, 100: 0 });
});
});

describe("#sell", function() {
it("entering 25, 25, 50 should return 'YES' as it can give change", function() {
ticketClerk.sell([25, 25, 50, 50]);
expect(ticketClerk.sell).toEqual("YES");
});
it("entering 50, 25, 50 should return 'NO' as it cannot give change", function() {
ticketClerk.sell([50, 25, 50]);
expect(ticketClerk.sell).toEqual("NO");
});
});
});

我遗漏了其他测试,我认为没有必要。 Kata 是关于接受 25 美元的电影票,并为一系列顾客找零。如果你可以给每个人零钱,它应该返回“YES”,如果不能,它应该返回“NO”。

最佳答案

您正在将方法名称而不是调用结果传递到 expect 语句中。您可以像这样简单地更改它:

describe("#TicketClerk", function() {
beforeEach(function() {
ticketClerk = new TicketClerk();
});
describe("#initialize", function() {
it("shows a hash of the money in the till, which starts with zero of each denominator", function() {
expect(ticketClerk.till).toEqual({ 25: 0, 50: 0, 100: 0 });
});
});

describe("#sell", function() {
it("entering 25, 25, 50 should return 'YES' as it can give change", function() {
const result = ticketClerk.sell([25, 25, 50, 50]); // pass result
expect(result).toEqual("YES");
});
it("entering 50, 25, 50 should return 'NO' as it cannot give change", function() {
const result = ticketClerk.sell([50, 25, 50]); // pass result
expect(result).toEqual("NO");
});
});
});

关于javascript - 测试未返回预期结果 - 售票员套路,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48725190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com