> ## Documentation Index
> Fetch the complete documentation index at: https://docs.devin.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 为支付服务添加单元测试

export const UseCaseHero = ({title, description, prompt, category, features, devinUrl, agent, intent, playbookId, type}) => {
  const encodedPrompt = encodeURIComponent(prompt || '');
  const tag = 'docs-use-case-gallery';
  const utm = 'utm_source=docs&utm_medium=use-case-gallery&utm_campaign=hero-cta';
  const agentParams = (agent ? '&agent=' + agent : '') + (intent ? '&intent=' + intent : '') + (playbookId ? '&playbookId=' + playbookId : '');
  const devinHref = type === 'schedule' ? 'https://app.devin.ai/settings/schedules/create?' + utm + agentParams + (prompt ? '&prompt=' + encodedPrompt : '') : type === 'review' ? 'https://app.devin.ai/review?' + utm : agent === 'ada' ? 'https://app.devin.ai/search?' + utm + '&noSubmit=true' + (prompt ? '&prompt=' + encodedPrompt : '') : devinUrl ? devinUrl.includes('?') ? devinUrl + '&' + utm + agentParams : devinUrl + '?' + utm + agentParams : prompt ? 'https://app.devin.ai/?tags=' + tag + '&' + utm + agentParams + '&prompt=' + encodedPrompt : 'https://app.devin.ai/?' + utm + agentParams;
  const buttonLabel = type === 'schedule' ? 'Schedule in Devin ↗' : type === 'review' ? 'Set Up Devin Review ↗' : agent === 'advanced' ? 'Try in Devin ↗' : agent === 'dana' ? 'Try in Dana ↗' : agent === 'ada' ? 'Try in Ask Devin ↗' : 'Try in Devin ↗';
  const featureList = features ? features.split(',').map(f => f.trim()) : [];
  return <div className="uc-hero">
      <div className="uc-hero-inner">
        <div className="uc-hero-left">
          <h1 className="uc-hero-title">{title}</h1>
          <p className="uc-hero-desc">{description}</p>
          <div>
            <a href={devinHref} target="_blank" rel="noopener noreferrer" className="try-in-devin-btn">
              {buttonLabel}
            </a>
          </div>
        </div>
        <div className="uc-hero-meta">
          <div className="uc-meta-item">
            <span className="uc-meta-label">Author</span>
            <span className="uc-meta-value">Cognition</span>
          </div>
          <div className="uc-meta-item">
            <span className="uc-meta-label">Category</span>
            <span className="uc-meta-value">{category}</span>
          </div>
          {featureList.length > 0 && <div className="uc-meta-item">
              <span className="uc-meta-label">Features</span>
              <span className="uc-meta-value">{featureList.join(', ')}</span>
            </div>}
        </div>
      </div>
    </div>;
};

export const PromptBlock = ({children, type, agent, intent, playbookId}) => {
  var utm = 'utm_source=docs&utm_medium=use-case-gallery&utm_campaign=prompt-block';
  var tag = 'docs-use-case-gallery';
  var agentParams = (agent ? '&agent=' + agent : '') + (intent ? '&intent=' + intent : '') + (playbookId ? '&playbookId=' + playbookId : '');
  var label = type === 'schedule' ? 'Schedule in Devin' : type === 'playbook' ? 'Create Playbook' : type === 'knowledge' ? 'Add to Knowledge' : agent === 'advanced' ? 'Try in Devin' : agent === 'dana' ? 'Try in Dana' : agent === 'ada' ? 'Try in Ask Devin' : 'Try in Devin';
  var buildUrl = function (text) {
    var encoded = encodeURIComponent(text);
    if (type === 'schedule') return 'https://app.devin.ai/settings/schedules/create?' + utm + agentParams + '&prompt=' + encoded;
    if (type === 'playbook') return 'https://app.devin.ai/settings/playbooks/create?' + utm + '&body=' + encoded;
    if (type === 'knowledge') return 'https://app.devin.ai/knowledge?' + utm + '&body=' + encoded;
    if (agent === 'ada') return 'https://app.devin.ai/search?' + utm + '&noSubmit=true&prompt=' + encoded;
    return 'https://app.devin.ai/?tags=' + tag + '&' + utm + agentParams + '&prompt=' + encoded;
  };
  const ref = React.useRef(null);
  const [href, setHref] = React.useState('#');
  React.useEffect(() => {
    if (!ref.current) return;
    var codeEl = ref.current.querySelector('pre code');
    if (codeEl) {
      var text = codeEl.textContent.trim();
      if (text) setHref(buildUrl(text));
    }
    var header = ref.current.querySelector('[data-component-part="code-block-header"]');
    if (header && !header.querySelector('.prompt-block-devin-link')) {
      var link = document.createElement('a');
      link.href = href;
      link.target = '_blank';
      link.rel = 'noopener noreferrer';
      link.className = 'prompt-block-devin-link';
      link.style.cssText = 'display:inline-flex;align-items:center;gap:6px;text-decoration:none;color:#fff;font-size:11px;font-weight:500;padding:4px 10px;border-radius:6px;white-space:nowrap;background:#317CFF;transition:background 0.2s;margin-left:8px;';
      link.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" y1="14" x2="21" y2="3"/></svg> ' + label;
      link.onmouseenter = function () {
        link.style.background = '#2968D9';
      };
      link.onmouseleave = function () {
        link.style.background = '#317CFF';
      };
      header.appendChild(link);
    }
    var existingLink = ref.current.querySelector('.prompt-block-devin-link');
    if (existingLink && href !== '#') existingLink.href = href;
  });
  return <div className="prompt-block" ref={ref}>{children}</div>;
};

<UseCaseHero title="为支付服务添加单元测试" description="为支付处理构建一套测试编写实践手册，由 Devin 使用全面的单元测试覆盖扣款流程、退款逻辑和 webhook 处理程序。" prompt="为 src/services/PaymentService.ts 中所有尚未测试的函数编写单元测试。覆盖 processCharge、issueRefund 和 handleWebhook。模拟 Stripe SDK 和数据库调用。每个函数都需要针对成功、错误，以及诸如重复扣款、部分退款等边界场景编写测试。目标是达到 90% 的代码行覆盖率。" category="代码质量" features="实践手册" />

<div className="uc-detail-wrapper">
  <Tip>不想手动配置？将本页链接粘贴到 Devin 会话中，让它为你完成所有设置。</Tip>

  <Steps>
    <Step title="编写支付场景专用测试手册">
      A [playbook](/zh/product-guides/creating-playbooks) 会将你团队的测试规范固化下来，让 Devin 编写测试的方式与工程师保持一致。支付相关代码有其独特考量——幂等性、货币精度、网关重试、符合 PCI 要求的 mocking 等——因此，一个专注支付场景的 playbook 能捕捉到通用 playbook 会遗漏的问题。

      **选项 1：自己编写 playbook。** 前往 [**Settings > Playbooks > Create playbook**](https://app.devin.ai/settings/playbooks/create?utm_source=docs\&utm_medium=use-case-gallery) 并定义你的规范：

      <PromptBlock type="playbook">
        ```txt Payments Test Playbook theme={null}
        ## Procedure
        1. Read the target payment module and identify all exported functions
        2. Study existing test files in src/services/__tests__/ for patterns
        3. Create a test file named {Module}.test.ts in __tests__/ next to the source
        4. Write tests using the AAA pattern (Arrange, Act, Assert)
        5. Mock the Stripe SDK with jest.mock('stripe') — never call real payment APIs
        6. Mock database calls with the helpers from src/test/helpers.ts
        7. Use integer cents for all currency values — never floating-point dollars
        8. Test idempotency: calling processCharge twice with the same idempotency key must not create duplicate charges
        9. Cover success paths, error paths, and edge cases for every function
        10. Run `npm test -- --coverage` and verify 90%+ line coverage for the file

        ## Specifications
        - Framework: Jest with TypeScript
        - Mocking: jest.mock() for Stripe SDK, jest.spyOn() for internal methods
        - Currency: Always use integer cents (e.g., 2999 not 29.99)
        - Assertions: Prefer specific matchers (toHaveBeenCalledWith, toThrow)
        - Naming: describe('PaymentService') > describe('processCharge') > it('charges a valid card')

        ## Forbidden Actions
        - Do not call real Stripe, PayPal, or any live payment gateway in tests
        - Do not use floating-point arithmetic for currency calculations
        - Do not modify the source module to make it easier to test
        - Do not skip idempotency or retry-logic edge cases
        ```
      </PromptBlock>

      **选项 2：让 Devin 为你创建 playbook。** 描述你的测试规范，Devin 会为你生成一个完整的 playbook：

      <PromptBlock agent="advanced">
        ```txt Generate a payments test playbook theme={null}
        Create a playbook for writing unit tests for our payment modules in
        src/services/. We use Jest with TypeScript, mock the Stripe SDK with
        jest.mock('stripe'), and use integer cents for all currency values.
        Look at our existing tests in src/services/__tests__/ to learn our
        patterns (AAA style, specific matchers, describe/it naming). The
        playbook should cover mocking strategy, idempotency testing, currency
        precision rules, and forbidden actions like calling real payment APIs.
        ```
      </PromptBlock>

      然后将你目前最好的一个测试文件 (例如 `src/services/__tests__/UserService.test.ts`) 添加为一条 [Knowledge](/zh/product-guides/knowledge) 条目，这样 Devin 就能获得一个你团队风格的具体示例。
    </Step>

    <Step title="定位未经测试的支付代码">
      在让 Devin 针对特定文件之前，先找出你的支付模块中存在哪些覆盖率薄弱环节。让 Devin 运行你的覆盖率工具，并找出问题最严重的部分：

      <PromptBlock>
        ```txt Analyze payment module coverage theme={null}
        Run npm test -- --coverage and list every file under src/services/
        that handles payments, billing, or subscriptions with less than 80%
        line coverage. Sort by coverage ascending so I can see the biggest
        gaps first.
        ```
      </PromptBlock>

      Devin 会在其终端中运行测试套件，解析覆盖率报告，并给出一份按优先级排序的列表：

      ```
      File                                | Lines | Uncovered functions
      ------------------------------------|-------|------------------------------
      src/services/PaymentService.ts      |  34%  | processCharge, issueRefund, handleWebhook
      src/services/SubscriptionService.ts |  41%  | renewSubscription, cancelTrial, proratePlan
      src/services/InvoiceService.ts      |  52%  | generateInvoice, applyPromoCode, calculateTax
      src/services/PayoutService.ts       |  58%  | initiateTransfer, reconcileSettlement
      ```
    </Step>

    <Step title="让 Devin 为 PaymentService 编写测试用例">
      新建一个新会话，附加你的支付测试 playbook (你会看到一个蓝色胶囊提示已附加) ，然后告诉 Devin 要针对哪个模块编写测试：

      <PromptBlock>
        ```txt Write tests for PaymentService theme={null}
        Write unit tests for all untested functions in
        src/services/PaymentService.ts. Focus on processCharge,
        issueRefund, and handleWebhook. Each function needs tests for
        success, error, and edge cases. Mock the Stripe SDK — never call
        real APIs. Use integer cents for all currency values. Run the
        tests and verify 90%+ line coverage for this file.
        ```
      </PromptBlock>

      Devin 会阅读该模块，学习你现有测试的模式和写法，按照你的 playbook 编写一份完整的测试文件，并运行它：

      ```
      PASS src/services/__tests__/PaymentService.test.ts
        PaymentService
          processCharge
            ✓ charges a valid credit card and returns a receipt (14ms)
            ✓ uses integer cents to avoid floating-point errors (6ms)
            ✓ rejects duplicate charges with the same idempotency key (5ms)
            ✓ retries on Stripe gateway timeout up to 3 times (18ms)
            ✓ throws InsufficientFundsError for declined cards (4ms)
          issueRefund
            ✓ refunds full amount for a completed charge (8ms)
            ✓ refunds partial amount in cents when specified (6ms)
            ✓ prevents refund exceeding the original charge amount (3ms)
            ✓ throws AlreadyRefundedError on duplicate refund attempts (4ms)
          handleWebhook
            ✓ processes charge.succeeded events and updates order status (7ms)
            ✓ processes charge.refunded events and credits the customer (6ms)
            ✓ verifies Stripe webhook signature before processing (3ms)
            ✓ ignores unrecognized event types without error (2ms)

      Coverage: 94% lines | 91% branches | 100% functions
      ```

      Devin 会创建一个 PR (pull request) ，并在描述中附上测试文件和覆盖率摘要。
    </Step>

    <Step title="依次处理剩余的支付模块">
      审查第一个拉取请求 (pull request，PR) 。如果 mock 策略或断言风格不太合适，在将 playbook 应用于更多模块之前先对其进行更新——这一轮反馈可以避免你在多个 PR 中反复修正同样的问题。

      然后按照你的覆盖缺口清单逐项推进：

      <PromptBlock>
        ```txt 覆盖 SubscriptionService theme={null}
        使用相同的 playbook 为 src/services/SubscriptionService.ts 编写测试。
        重点覆盖 renewSubscription、cancelTrial 和 proratePlan。
        使用以分为单位的整数来测试按比例分摊的计算。目标是达到 90% 以上的行覆盖率。
        ```
      </PromptBlock>

      为了更快推进，让 Devin 启动并行会话——每个支付模块一个会话——全部遵循同一套 playbook。或者 [安排](/zh/product-guides/scheduled-sessions) 每周定期会话，自动查找任何低于覆盖率阈值的支付模块并为其生成测试。
    </Step>
  </Steps>
</div>
