繁体   English   中英

如何存根从柏树中的模块导出的 function?

[英]How to stub exported function from module in cypress?

从赛普拉斯调用getUser时,我无法看到getName方法的存根响应。 有没有办法纠正这个问题?

// Service.ts    
export const getName = (): string => {
    return name;
}

// User.ts
import {getName} from './Service'
export const getUser = (): User => {
    const name = getName();
    // ... rest of code for User creation 
}

// User.cypress.ts
import * as service from './Service'
it('tests user', () => {
    cy.stub(service, 'getName').returns('abc');
    cy.get('#get-user-id').click(); 
});

您可能需要更改 function 从Service.ts导出的方式。

尝试向模块添加默认导出。

// Service.ts    
const getName = (): string => {
    return name;
}

module.exports {
  getName
}
// User.cypress.ts
import service from './Service'

it('tests user', () => {
    cy.stub(service, 'getName').returns('abc');
    cy.get('#get-user-id').click(); 
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM