[英]Spy on a field assignment using Jasmine
How could I spy on a field assignment using Jasmine?我如何使用 Jasmine 监视现场作业?
Eg I have the object under test:例如,我正在测试 object:
const testObj = {
testObjField: null,
testObjFunc: function() { this.testObjField = "foo"; }
}
Now I want to assert that the testObj.testObjFunc
will assign the "foo"
to the testObj.testObjField
.现在我想断言
testObj.testObjFunc
会将"foo"
分配给testObj.testObjField
。 How should I proceed?我该如何进行?
Here is what I tried:这是我尝试过的:
//arrange
const testObj = {
testObjField: null,
testObjFunc: function () {
this.testObjField = 'foo';
}
};
const testObjTestObjFieldSpy = spyOnProperty(testObj, 'testObjField', 'set');
//act
testObj.testObjFunc();
//assert
expect(testObjTestObjFieldSpy).toHaveBeenCalledWith('foo');
But I am getting the error:但我收到错误:
Error: : Property testObjField does not have access type set
错误::属性 testObjField 没有设置访问类型
You can't spy on fields, only on methods.您不能监视字段,只能监视方法。
I would do this:我会这样做:
//arrange
let testObj = {
testObjField: null,
testObjFunc: function () {
this.testObjField = 'foo';
}
};
// !! Get a copy of the original
const originalTestObj = { ...testObj };
//act
testObj.testObjFunc();
//assert
expect(testObj.testObjField).toBe('foo');
// !! Reset testObj to what it was if that is required
testObj = { ...originalTestObj };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.