1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【保険商品管理システムの開発】生命保険のコントローラーのテスト

Posted at

1. 顧客登録

スクリーンショット 2025-08-30 7.55.36.png

2. 保険契約登録

スクリーンショット 2025-08-30 7.56.54.png

3. 有効契約取得

GET https://localhost:5001/api/LifeInsurance/active-policies/CUST001

エラー

System.InvalidOperationException: The LINQ expression 'DbSet<InsurancePolicy>()
    .Where(i => i.CustomerId == __customerId_0 && i.IsActive && !(i.IsExpired()))' could not be translated. 

👉 EF Core が IsExpired() という C# メソッドを SQL に変換できないために失敗しています。

Entity Framework Core は C# のメソッドをそのまま SQL に変換できるわけではないので、IsExpired() のような独自メソッドをクエリ式 (Where(...)) に含めるとこのエラーになります。

[HttpGet("active-policies/{customerId}")]
public async Task<IActionResult> GetActivePoliciesByCustomer(string customerId)
{
    var activePolicies = await _context.InsurancePolicies
        .Where(p => p.CustomerId == customerId && p.IsActive)// ← && !p.IsExpired()を削除
        .ToListAsync(); // ここで SQL を実行してメモリにロード

    // ここからは LINQ to Objects (C#) なので IsExpired() が使える
    activePolicies = activePolicies
        .Where(p => !p.IsExpired())
        .ToList();

    return Ok(activePolicies);
}

➡️ ToListAsync() で一旦 DB から取得してから、C# 側で IsExpired() を適用する流れです。
データ量が多いときは少し効率が落ちますが、簡単に動作します。

再テスト成功

スクリーンショット 2025-08-30 8.08.19.png

4. 保険料合計

GET https://localhost:5001/api/LifeInsurance/total-premium/CUST001

エラー

/Users/kerumatomomitsu/InsuranceProductManager/Controllers/LifeInsuranceController.cs(85,18): error CS1061: 'decimal' に 'Where' の定義が含まれておらず、型 'decimal' の最初の引数を受け付けるアクセス可能な拡張メソッド 'Where' が見つかりませんでした。using ディレクティブまたはアセンブリ参照が不足していないことを確認してください

問題の部分 👇

var totalPremium = await _context.InsurancePolicies
    .Where(p => p.CustomerId == customerId && p.IsActive)
    .SumAsync(p => p.Premium);

// ❌ totalPremium は decimal 型なので .Where() は呼べない
totalPremium = totalPremium
    .Where(p => !p.IsExpired())
    .ToList();

修正版

/// <summary>
/// 保険料の合計を算出
/// </summary>
[HttpGet("total-premium/{customerId}")]
public async Task<IActionResult> CalculateTotalPremium(string customerId)
{
    // まず契約を取得(DBからは IsExpired() を判定できないので有効契約だけ持ってくる)
    var policies = await _context.InsurancePolicies
        .Where(p => p.CustomerId == customerId && p.IsActive)
        .ToListAsync();

    // ここから C# 側で IsExpired() を使ってフィルタリング
    var totalPremium = policies
        .Where(p => !p.IsExpired())
        .Sum(p => p.Premium);

    return Ok(totalPremium);
}

再テスト:成功

スクリーンショット 2025-08-30 8.21.47.png

5. 保険金請求

POST https://localhost:5001/api/LifeInsurance/claim/POL001

スクリーンショット 2025-08-30 8.26.38.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?