简体   繁体   中英

I am using a sinon.stub method to wrap my service in typescript which expects an array however my mock data is an object how do I fix this

This is the service I am stubing


class TransactionService {
  async getTransactions(
    req: RequestContract,
    query: TransactionQuery
  ): Promise<Transaction[]> {
    const token = await Auth.headless({ nuban: query.nuban });
    const iris = new Iris({ req, token, headless: true, retries: 1 });

    return iris.get<Transaction[]>(`${env.proxy_url}/transactions/`, {
      params: query
    });
  }

  filterTransaction(req: Request, transactions: Transaction[]): Transaction[] {
    const accounts = req.session.accounts.map(account => account.acccount_number);
    return transactions.filter(transaction => accounts.includes(transaction.nuban));
  }
}

export const TransactionsService = new TransactionService();

Here is my stub method


export function mockGetTransactions() {
    sinon.stub(TransactionsService, 'getTransactions').resolves(mockTransactions('0071326981')) 
}

Finally here is the mock data

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,
  };
}

This is the error I am getting

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'))

So getTransactions is expecting Transaction[] ie Transaction array, while I am passing an object with mockTransactions

It looks like your getTransactions method is expecting a Transactions[] so you should be able to modify your mock data to return an array containing your transaction object.

export function mockTransactions(account: string): Transaction[] {
  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,
    }
  ];
}

So this fixed it for me

I created an array function

export function array<T>(size: number, fake: () => T): T[] {
  const values = [];
  for (let i = 0; i < size; i++) values.push(fake());

  return values;
}

I assigned a type Transaction to my mock

export function mockTransaction(): Transaction {
  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: '0071326981',
  };
}

And then returned my array function here

export function mockTransactions(num: number): Transaction[] {
  return array(num, mockTransaction);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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