簡體   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