Upgrade to Pro — share decks privately, control downloads, hide ads and more …

名単体テスト 禁断の傀儡(モック)

名単体テスト 禁断の傀儡(モック)

2025-05-14
シネマ de LT会〜あなたのナレッジ大上映〜
https://aeon.connpass.com/event/352070/

Avatar for iwamot

iwamot

May 14, 2025
Tweet

More Decks by iwamot

Other Decks in Technology

Transcript

  1. 名単体テスト 名単体テスト ~ 禁断の ~ 禁断の傀儡 傀儡 ~ ~ モ

    ッ ク モ ッ ク 2025.5.14 シネマ de L T会@イオンシネマ シアタス調布 2025.5.14 シネマ de LT会@イオンシネマ シアタス調布 https:/ /aeon.connpass.com/event/352070/ https://aeon.connpass.com/event/352070/
  2. @patch("app.bolt_listeners.find_parent_message", return_value=None) @patch("app.bolt_listeners.is_this_app_mentioned") def test_is_child_message_and_mentioned_no_channel_id( mock_is_mentioned, mock_find_parent, mock_client, mock_context ):

    mock_context.channel_id = None assert is_child_message_and_mentioned( mock_client, mock_context, "12345") is False mock_find_parent.assert_not_called() mock_is_mentioned.assert_not_called() ―― モックだらけじゃねえか ―― モックだらけじゃねえか こんなもん 受け入れていいのか? こんなもん 受け入れていいのか?
  3. bmi :: Float -> Float -> Float bmi weight height

    = weight / height ^ 2 なら モックのいらない実装に変えりゃいい なら モックのいらない実装に変えりゃいい 理想は「副作用のない純粋関数」さ 理想は「副作用のない純粋関数」さ
  4. def build_slack_user_prefixed_text(reply: dict, text: str) -> str: user_identifier = reply.get("user",

    reply.get("username")) return f"<@{user_identifier}>: {text}" 光の見えた俺は Collmboに 光の見えた俺は Collmboに 純粋関数を増やしてった 純粋関数を増やしてった
  5. @pytest.mark.parametrize( "reply, text, expected", [ ({"user": "U123"}, "hello", "<@U123>: hello"),

    ({"username": "someone"}, "hi", "<@someone>: hi"), ({}, "yo", "<@None>: yo"), ], ) def test_build_slack_user_prefixed_text(reply, text, expected): assert build_slack_user_prefixed_text(reply, text) == expected AIの書いたテストも 今なら AIの書いたテストも 今なら 自信を持って受け入れられるぜ 自信を持って受け入れられるぜ