it('returns the median for even number of grades', () => {
const grades = [80, 90, 85, 110, 100, 60]
const expectedMedian = 87.50
const median = calculateMedian(grades)
expect(median).toBe(expectedMedian)
})
这个测试通过了,但我不希望
87.5
与 87.50
相等。
如何处理这个问题取决于
calculateMedian
返回的内容。如果您要比较浮点数,请使用 .toBeCloseTo(number, numDigits?)
。
it('returns the median for even number of grades', () => {
const grades = [80, 90, 85, 110, 100, 60]
const expectedMedian = 87.50
const median = calculateMedian(grades)
expect(median).toBeCloseTo(expectedMedian)
});
如果
calculateMedian
返回一个字符串(根据注释似乎是这种情况),那么您可以使用 .toBe()
,但使 expectedMedian
成为一个字符串:
it('returns the median for even number of grades', () => {
const grades = [80, 90, 85, 110, 100, 60]
const expectedMedian = '87.50'
const median = calculateMedian(grades)
expect(median).toBe(expectedMedian)
});