假设我有这个功能
function changeFooBarToOne(foo) {
foo.bar = 1;
}
如何测试它是否将值更改为 1?
describe('changeFooBarToOne', () => {
it('modifies the bar property value to 1', () => {
const foo = { bar: 0 };
// call the expect and evaluate foo to equal { bar: 1 }
})
})
最佳答案
不要忘记,JavaScript 对象是引用类型。
function changeFooBarToOne(foo) {
foo.bar = 1;
}
describe('changeFooBarToOne', () => {
it('modifies the bar property value to 1', () => {
const foo = { bar: 0 };
changeFooBarToOne(foo);
expect(foo.bar).toBe(1);
})
})
//another way
describe('changeFooBarToOne_1', () => {
it('modifies the bar property value to 1', () => {
const foo = { bar: 0 };
changeFooBarToOne(foo);
expect(foo).toEqual({bar : 1});
})
})
关于javascript - Jest - 如何测试函数是否对参数进行了更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50631642/