[英]Mock reassigning properties in mocked interface
I have the following interface which I'm trying to mock:我有以下我试图模拟的界面:
export interface IEmail {
from: string;
body: string;
to: string;
}
This interface is used in a function like:此接口用于以下函数:
async function sendEmail(emailData: IEmail): Promise<void> {
await this.send({
emailBody: emailData.body,
emailBodyFrom: emailData.from,
emailTo: emailData.to,
});
}
async function send(email) {
verifyEmail(email)
emailprovider.send(email)
}
I tried mocking the interface with jest-mock-extended
like this:我尝试像这样用jest-mock-extended
接口:
it('should send email', async () => {
const options = mock<IEmail>({
emailBody: "You got mail",
from: "marc@meta.com",
to: "jane.doe@gmail.com",
});
mockemailService.send.mockResolvedValueOnce("");
await emailService.sendEmail(options);
expect(mockemailService.send).toHaveBeenCalledWith({
emailBody: 'You got mail',
emailBodyFrom: 'marc@meta.com',
emailTo: 'jane.doe@gmail.com',
});
});
Running the test gives the following diff:运行测试给出以下差异:
- emailBody: 'You got mail',
- emailBodyFrom: 'marc@meta.com',
- emailTo: 'jane.doe@gmail.com',
+ emailBody: undefined,
+ emailBodyFrom: undefined,
+ emailTo: undefined,
I have debugged and it seems like when I copy the vales from one object to another emailBody: emailData.body
is causing the value to be undefined.我已经调试过,当我将值从一个对象复制到另一个emailBody: emailData.body
导致值未定义。
Is there a better way to create the mock perhaps?也许有更好的方法来创建模拟吗?
I was able to resolve it by setting the fields like this instead:我能够通过设置这样的字段来解决它:
const options = mock<IEmail>({});
options.emailBody = "You got mail"
options.from = "marc@meta.com"
options.to = "jane.doe@gmail.com"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.