首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Jest测试雀巢的paypal启动

用Jest测试雀巢的paypal启动
EN

Stack Overflow用户
提问于 2022-10-11 16:35:14
回答 1查看 59关注 0票数 0

我正在为NestJs中的服务方法编写测试,该方法启动Paypal支付意图。我运行yarn test:watch或简单的yarn test命令,下面编写的测试运行并通过,但是在测试完成后,它将引发以下错误。

TEST

代码语言:javascript
复制
describe("PaymentsService", () => {
  let paymentsService: PaymentsService
  let paymentRepository
  let ordersService

  beforeEach(async () => {
    const module = await Test.createTestingModule({
      imports: [
        StripeModule.forRoot({
          apiKey: "stripe.apiKey",
          apiVersion: "2020-08-27",
        }),
      ],
      providers: [
        PaymentsService,
        { provide: PaymentRepository, useFactory: mockPaymentRepository },
        { provide: UsersService, useFactory: mockUsersService },
        { provide: OrdersService, useFactory: mockOrdersService },
        {
          provide: Stripe,
          useFactory: () => ({
            checkout: {
              sessions: {
                create: jest.fn(),
              },
            },
          }),
        },
      ],
    }).compile()

    paymentsService = module.get<PaymentsService>(PaymentsService)
    paymentRepository = module.get<PaymentRepository>(PaymentRepository)
    ordersService = module.get<OrdersService>(OrdersService)
  })

  describe("initiatePaypalPayment", () => {
    it("will initiate paypal payment", async () => {
      ordersService.getOrder.mockResolvedValue(mockOrder)
      ordersService.calculateOrderPrice.mockResolvedValue(mockOrder.price)

      jest.doMock("paypal-rest-sdk", () => {
        return jest.fn(() => ({
          payment: {
            create: jest.fn(() => Promise.resolve({})),
          },
        }))
      })

      expect(
        await paymentsService.initiatePaypalPayment(
          { orderId: 10, voucherCode: "string" },
          mockedResponse
        )
      ).toEqual(undefined)
    })
  })
})

TEST RESULTS

代码语言:javascript
复制
 PASS  src/payments/payments.service.spec.ts
  PaymentsService
    initiatePaypalPayment
      ✓ will initiate paypal payment (117ms)

  console.log src/payments/payments.service.ts:72
    initiatePaypalPayment

  console.log src/payments/payments.service.ts:86
    totalPrice:  1000

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        4.926s, estimated 6s
Ran all test suites related to changed files.

Watch Usage
 › Press a to run all tests.
 › Press f to run only failed tests.
 › Press p to filter by a filename regex pattern.
 › Press t to filter by a test name regex pattern.
 › Press q to quit watch mode.
 › Press Enter to trigger a test run.

  ●  Cannot log after tests are done. Did you forget to wait for something async in your test?
    Attempted to log "Error in paypap Error: Response Status : 401
        at IncomingMessage.<anonymous> (/home/user/Desktop/app/node_modules/paypal-rest-sdk/lib/client.js:130:23)
        at IncomingMessage.emit (events.js:326:22)
        at IncomingMessage.EventEmitter.emit (domain.js:483:12)
        at endReadableNT (_stream_readable.js:1241:12)
        at processTicksAndRejections (internal/process/task_queues.js:84:21) {
      response: {
        error: 'invalid_client',
        error_description: 'Client Authentication failed',
        httpStatusCode: 401
      },
      httpStatusCode: 401
    }".

      91 |         payment_method: 'paypal',
      92 |       },
    > 93 |       redirect_urls: {
         |                         ^
      94 |         return_url: 'www.example.com',
      95 |         cancel_url: 'www.example.com',
      96 |       },

      at IncomingMessage.<anonymous> (../node_modules/paypal-rest-sdk/lib/client.js:130:23)
      at processTicksAndRejections (../internal/process/task_queues.js:84:21) {
        response: {
          error: 'invalid_client',
          error_description: 'Client Authentication failed',
          httpStatusCode: 401
        },
        httpStatusCode: 401
      }".
      at console.log (../node_modules/@jest/console/build/CustomConsole.js:183:10)
      at payments/payments.service.ts:93:25
      at ../node_modules/paypal-rest-sdk/lib/api.js:102:13
      at ../node_modules/paypal-rest-sdk/lib/api.js:87:9
      at IncomingMessage.<anonymous> (../node_modules/paypal-rest-sdk/lib/client.js:140:13)

/home/user/Desktop/app/src/payments/payments.service.ts:94
                throw new common_1.InternalServerErrorException();
                ^

InternalServerErrorException: Internal Server Error
    at /home/user/Desktop/app/src/payments/payments.service.ts:115:15
    at /home/user/Desktop/app/node_modules/paypal-rest-sdk/lib/api.js:102:13
    at /home/user/Desktop/app/node_modules/paypal-rest-sdk/lib/api.js:87:9
    at IncomingMessage.<anonymous> (/home/user/Desktop/app/node_modules/paypal-rest-sdk/lib/client.js:140:13)
    at IncomingMessage.emit (events.js:326:22)
    at IncomingMessage.EventEmitter.emit (domain.js:483:12)
    at endReadableNT (_stream_readable.js:1241:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  response: { statusCode: 500, message: 'Internal Server Error' },
  status: 500
}
error Command failed with exit code 1.

是因为我没有以正确的方式模拟paypal-rest-sdk,还是有可能是另一个错误的原因。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2022-10-11 18:28:39

发生来自invalid_client API的401 PayPal /‘客户端身份验证失败’错误有两个原因之一:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74031380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档