Sails.js:玩笑间谍会导致 TypeError:无法分配给只读属性
我试图使用Jest的Spyon
嘲笑函数调用的实现:
await sails.helpers.models.test.randomFn.with({ ... });
const randomFnSpy = jest.spyOn(sails.helpers.models.test.randomFn, 'with');
randomFnSpy.mockImplementation(() => {});
错误:
typeError:无法分配使用“ of function”函数runfn(_argins,_explicliccbmaybe,_metadata){
我尝试将属性设置为配置
和wartial> wortable
:
Object.defineProperty(
sails.helpers.models.test.randomFn,
'with',
{ configurable: true, writable: true }
);
错误:
TypeError:无法重新定义属性: 在function.defineproperty()
I'm trying to mock the implementation of a function call using Jest's spyOn
:
await sails.helpers.models.test.randomFn.with({ ... });
const randomFnSpy = jest.spyOn(sails.helpers.models.test.randomFn, 'with');
randomFnSpy.mockImplementation(() => {});
Error:
TypeError: Cannot assign to read only property 'with' of function 'function runFn(_argins, _explicitCbMaybe, _metadata){
I tried setting the property as configurable
and writable
:
Object.defineProperty(
sails.helpers.models.test.randomFn,
'with',
{ configurable: true, writable: true }
);
Error:
TypeError: Cannot redefine property: with
at Function.defineProperty ()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说,模拟助手的方法是:
Sails 在内部使用
machine
,其中执行以下操作:不是监视
with
,而是嘲笑助手的fn
达到了目的:What worked to mock helpers for me:
Sails internally uses
machine
which does the following:Instead of spying on
with
, mocking the helper'sfn
did the trick: