export function mockGetTransactions() {
sinon.stub(TransactionsService, 'getTransactions').resolves(mockTransactions('0071326981'))
}
最后是模拟数据
export function mockTransactions(account: string) { return { date: faker.date.recent(3), transaction_type: 'credit', amount: mockAmount(10000, 1000000, 4), remark: faker.random.words(10), reference: faker.random.words(5), nuban: account, }; }
这是我得到的错误
TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option): __tests__/mocks/services/transaction.ts:6:65 - error TS2345: Argument of type '{ date: Date; transaction_type: string; amount: number; remark: string; reference: string; nuban: string; }' is not assignable to parameter of type 'Transaction[]'. Type '{ date: Date; transaction_type: string; amount: number; remark: string; reference: string; nuban: string; }' is missing the following properties from type 'Transaction[]': length, pop, push, concat, and 26 more. 6 sinon.stub(TransactionsService, 'getTransactions').resolves(mockTransactions('0071326981'))
所以
getTransactions
期望使用Transaction []即Transaction数组,而我正在传递带有模拟交易的对象
这是我正在存根类服务Service {看起来您的getTransactions方法期望使用事务[],因此您应该能够修改模拟数据以返回包含事务对象的数组。