> ## 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](/ja/product-guides/creating-playbooks) は、チームのテスト方針や慣習を定義しておくことで、Devin がエンジニアと同じ流儀でテストを書けるようにするものです。決済コードには、冪等性、通貨の精度、ゲートウェイのリトライ、PCI 準拠の安全なモックなど、特有の考慮事項があります。そのため、決済に特化した 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](/ja/product-guides/knowledge) のエントリとして追加し、Devin が参照できるようにします。
    </Step>

    <Step title="未テストの決済コードを特定する">
      特定のファイルをDevinに指定する前に、まず決済モジュールのどこにカバレッジの抜け漏れがあるかを把握しましょう。Devinにカバレッジツールを実行させて、最も問題の大きい箇所を洗い出します：

      <PromptBlock>
        ```txt 支払いモジュールのカバレッジを分析 theme={null}
        npm test -- --coverage を実行し、src/services/ 配下のうち
        payments、billing、subscriptions を扱っていて行カバレッジが 80% 未満の
        ファイルをすべて列挙してください。カバレッジが低い順に並べ替えて、
        最もギャップが大きいものから確認できるようにしてください。
        ```
      </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 のテストを作成させる">
      新しいセッションを開始し、決済テスト用プレイブックを添付します (添付されると、接続済みを示す青いピル状の表示が出ます) 。そのうえで、どのモジュールをカバーするかを 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 はモジュールを読み込み、既存のテストを確認してパターンを把握し、あなたのプレイブックに従った網羅的なテストファイルを作成して実行します:

      ```
      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 を作成します。
    </Step>

    <Step title="残りの決済モジュールに取り組んでください">
      最初のPRをレビューします。モックの戦略やアサーションのスタイルがしっくりこない場合は、他のモジュールでプレイブックを実行する前に更新してください。最初の1回でフィードバックを反映しておけば、同じ問題を複数のPRで繰り返し修正せずに済みます。

      次に、ギャップリストを上から順に埋めていきます。

      <PromptBlock>
        ```txt Cover SubscriptionService theme={null}
        src/services/SubscriptionService.ts 向けに、同じプレイブックを使って
        テストを書いてください。renewSubscription、cancelTrial、および proratePlan
        にフォーカスします。日割り（プランの按分）計算は整数のセント単位でテストしてください。
        行カバレッジ 90%以上を目標にします。
        ```
      </PromptBlock>

      より速く進めるには、Devin に支払いモジュールごとに並列セッションを立ち上げさせ、いずれも同じプレイブックに従わせます。または、週次セッションを[スケジュール](/ja/product-guides/scheduled-sessions)しておき、カバレッジのしきい値を下回った支払いモジュールを検出して、自動的にテストを書かせることもできます。
    </Step>
  </Steps>
</div>
