1. はじめに
この Codelab では、コードを実装する前にテストを作成するテスト駆動開発(TDD)の原則を紹介します。このアプローチを示すために、Ruby を使用して簡単な計算機アプリを作成します。
また、コーディング アシスタントとして Gemini を使用する方法についても学習します。Gemini は、テストケースの生成、コード実装の提案、さらにはコードの説明を提供して TDD サイクルを加速します。
最後に、Google のフルマネージド サーバーレス プラットフォームである Cloud Functions に計算機アプリをデプロイして、アプリにアクセスできるようにする方法を学びます。
2. 前提条件
- 課金が有効になっている Google Cloud プロジェクト
- ウェブブラウザ
プロジェクトを作成する
- Google Cloud コンソールのプロジェクト選択ページで、Google Cloud プロジェクトを選択または作成します。
- Cloud プロジェクトで課金が有効になっていることを確認します。詳しくは、プロジェクトで課金が有効になっているかどうかを確認する方法をご覧ください。
Gemini API を有効にする
- Gemini Marketplace にアクセスして API を有効にします。gcloud を使用して API を有効にすることもできます。
gcloud services enable cloudaicompanion.googleapis.com --project PROJECT_ID
- Gemini のページにアクセスして、[Start chatting] をクリックします。
- 重要: Gemini の使用を開始し、Cloud Shell IDE で Gemini を有効にするには、この Codelab の手順 1 と 2 に沿って操作してください。
Cloud Functions API を有効にする
- Google Cloud コンソールのプロジェクト選択ページで、Google Cloud プロジェクトを選択または作成します。
- Cloud Functions API ページに移動して、API を有効にします。gcloud を使用して API を有効にすることもできます。
gcloud services enable cloudfunctions.googleapis.com --project PROJECT_ID
Cloud Shell をアクティブにする
- Cloud Shell(Google Cloud コンソールで実行されるコマンドライン環境)を使用します。この環境には、必要な言語がプリロードされています。Cloud コンソールで、右上にある [Cloud Shell をアクティブにする] をクリックします。

- Cloud Shell に接続すると、すでに認証は完了しており、プロジェクトに各自のプロジェクト ID が設定されていることがわかります。Cloud Shell で次のコマンドを実行して、認証されたことを確認します。
gcloud auth list
- Cloud Shell で次のコマンドを実行して、gcloud コマンドがプロジェクトを認識していることを確認します。
gcloud config list project
- プロジェクトが設定されていない場合は、次のコマンドを使用して設定します。
gcloud config set project <YOUR_PROJECT_ID>
gcloud コマンドとその使用方法については、ドキュメントをご覧ください。
エディタを開く
- この Codelab では、組み込みの Cloud エディタを使用します。Cloud Shell ウィンドウで、右上にある [エディタを開く] ボタンをクリックします。VSCode エディタが開きます。

Ruby 拡張機能を追加する
- 左側のメニューで拡張機能アイコン
をクリックし、検索バーに vscode rubyと入力して、VSCode Ruby 拡張機能をインストールします。

3. Gemini ウォームアップ
LLM は非決定的です。そのため、これらのプロンプトを試しているときに受け取るレスポンスが、Codelab に添付されているスクリーンショット内のレスポンスとは異なる場合があります。
まず、Gemini に TDD とは何かを尋ねてみましょう。同じ Cloud Shell エディタで Gemini との会話を開始するには、次の手順を行います。
- ページの左側にある Gemini アイコン
を見つけます。これをクリックすると、左側にチャット ウィンドウが開きます。 Enter a prompt hereと表示されているテキスト ボックスに入力を開始できます。

プロンプト:
What is Test Driven Development?
対応:
Test-driven development (TDD) is a software development methodology that requires developers to write automated tests before writing any production code. This helps ensure the code is correct and meets the requirement. ....
TDD とそのメリットについて簡単に説明します。
Gemini に Ruby とは何かを尋ねてみましょう。
プロンプト:
What is Ruby?
対応:
Ruby is a general-purpose, interpreted, object-oriented programming language. It is designed to be easy to read and write. Ruby is dynamically typed, meaning that the type of a variable is not declared and can change during execution. ....
Ruby の概要と、言語として Ruby を使用するメリットについて説明します。
4. プロジェクトの設定
簡単な Sinatra アプリを作成してみましょう。
- 空のフォルダを作成しましょう。任意の名前を付けることができます。この Codelab では、これを
gemini-tddと呼びます。左側のクラウド エディタの [エクスプローラ] タブで、/home/<YOUR-USERNAME>/の下にフォルダを作成します。

- 作成したフォルダを開きましょう。左端のメニューでアイコン
をクリックし、[ファイル] -> [フォルダを開く] をクリックします。 - このルートフォルダに、アプリの開始点となる新しい Ruby ファイルを作成します。ファイル名は任意です。この Codelab では、これを
app.rbと呼びます。
- ルートフォルダに
Gemfileも追加しましょう。Gemfileについても Gemini に尋ねてみましょう。 - また、テストケースを記述するための
app_test.rbファイルも作成します。
5. Gemini を活用した実装
まず、Gemini に、何をしようとしているのかというコンテキストを提供し、同じテストケースを生成するようリクエストします。Gemini とのチャットのプロンプトで、次のプロンプトをコピーします。
プロンプト:
Answer as a software engineer with Ruby expertise. We want to build a Ruby calculator application with a test driven development approach. Let's start by writing some tests for the calculator app using minitest gem.
対応:
Here is a simple test for a calculator app that adds two numbers:
require "minitest/autorun"
describe "Calculator" do
it "should add two numbers" do
calculator = Calculator.new
assert_equal 5, calculator.add(2, 3)
end
end
This test uses the assert_equal method to verify that the result of the add method is correct. .....
電卓アプリのサンプル テストケース(2 つの数値を加算するテストケース)が提供されます。また、テストに必要な依存関係 minitest/autorun もインポートしました。コンテンツを app_test.rb にコピーしましょう。このテストを実行してみましょう。コードが記述されていないため、確実に失敗します。
このテストを実行する方法を Gemini に尋ねてみましょう。
プロンプト:
How do we run the tests written in app_test.rb using ruby command?
対応:
To run the tests written in app_test.rb using the ruby command, you can use the following steps: 1.Make sure that you are in the same directory as the app_test.rb file. 2.Open a terminal window. 3.Type the following command:
ruby app_test.rb
This will run all of the tests in the app_test.rb file and report the results.
では、テストを実行してみましょう。[エクスプローラー] タブで app_test.rb を右クリックし、[統合ターミナルで開く] をクリックします。

ファイル app_test.rb と同じフォルダにいることを確認します。そうでない場合は、cd コマンドを使用して移動します。ターミナルで、Gemini から返されたコマンドを実行します。
ruby app_test.rb
次のエラーが発生します。
NameError: uninitialized constant Calculator
エディタから直接 Gemini に修正方法を尋ねてみましょう。Cloud Shell エディタのナビゲーション ペインにあるチャット アイコンをクリックし、次のプロンプトを入力します。
プロンプト:
NameError: uninitialized constant Calculator. Give me Calculator class code.
対応:
The error you are getting is because you have not defined the Calculator class. You need to define the Calculator class before you can use it in your tests. Here is the code for the Calculator class:
Class Calculator
def add(a, b)
a + b
end
end
Once you have defined the Calculator class, you can run the tests again and they will pass. .....
Gemini は、Calculator というクラスがないため、作成する必要があることを示しています。また、Calculator クラスを作成するためのコードも提供されています。このコードを使用しましょう。[エクスプローラ] タブから app.rb を開き、Gemini が提示したコードをコピーします。app.rb のコンテンツを変更しているときに、Gemini から予測入力の候補が表示されることがあります。候補を承認する場合は tab を押し、承認しない場合は Esc を押します。
もう一度テストを実行してみましょう。統合ターミナルで次のコマンドを入力します。
ruby app_test.rb
しかし、同じエラー「NameError: uninitialized constant Calculator」が発生します。Gemini に修正方法を尋ねてみましょう。
6. Gemini を使用した反復 TDD
プロンプト:
I am still getting this error: "NameError: uninitialized constant Calculator".
対応:
The error you are getting is because you have not defined the Calculator class. You need to define the Calculator class before you can use it in your tests. Here is the code for the Calculator class:
class Calculator
def add(a, b)
a + b
end
def subtract(a, b)
a - b
end
def multiply(a, b)
a * b
end
def divide(a, b)
a / b
end
end
....
上記と同じかどうかはわかりませんが、何らかのレスポンスが返されました。そこで、app.rb で Calculator クラスが定義されていることを Gemini に明示的に伝えます。
プロンプト:
Calculator class is defined in app.rb already.
対応:
I see that the Calculator class is defined in the app.rb file. However, you are getting the NameError error because you are trying to use the Calculator class in the app_test.rb file without requiring the app.rb file. To fix this, you need to add the following line to the top of the app_test.rb file:
require_relative "app"
Gemini から、Calculator クラスが定義されている app.rb をインポートするレスポンスが返されました。適切な回答を得るまでに 2 ~ 3 回のプロンプトの繰り返しが必要になることもありますが、慣れてくると精度が向上します。Gemini からの回答で require "./app" が表示されることもあります。これを app_test.rb の app.rb に追加して、同じテストを再度実行してみましょう。
この時点で app_test.rb が次のようになっていることを確認してください。
require "minitest/autorun"
require "./app"
describe "Calculator" do
it "should add two numbers" do
calculator = Calculator.new
assert_equal 5, calculator.add(2, 3)
end
end
プロジェクト フォルダ内の Cloud Shell ターミナルから次のコマンドを実行します。
ruby app_test.rb
すべてがうまくいけば、テストは合格するはずです。そこで、TDD アプローチを使用して非常にシンプルな Ruby アプリを開発し、Gemini の支援を受けました。プロンプトの回答は常に同じになるわけではありません。これは LLM モデルの性質によるものです。Gemini から電卓の操作がさらに表示される可能性がありますが、全体的には先ほどの手順で問題ありません。
7. Cloud Functions にアプリをデプロイする
次に、Gemini にこの関数を Cloud Functions にデプロイするように依頼します。
プロンプト:
Let's deploy this app to Cloud Functions
対応:
To deploy your app to Cloud Functions, you can use the following steps: 1.Create a new Cloud Functions project. 2.Create a new Cloud Function. 3.Set the trigger for the Cloud Function to be an HTTP request. 4.Set the entry point for the Cloud Function to be the app method in the app.rb file. 5.Deploy the Cloud Function. Once you have deployed the Cloud Function, you can test it by sending an HTTP request to the function's URL. ....
Gemini は、Cloud Functions UI を使用して手動でデプロイする方法を説明します。でも、それはしたくありません。Gemini に gcloud コマンドを教えてもらい、統合ターミナルから直接実行してみましょう。
プロンプト:
How do I deploy this app to Cloud Functions gen2 using the gcloud command?
対応:
...
Here are the specific commands you can use to deploy your app to Cloud Functions using the gcloud command:
gcloud functions deploy calculator --runtime ruby32 --trigger-http --entry-point app
This command will create a new Cloud Function called calculator that is triggered by an HTTP request and uses the app method in the app.rb file as the entry point.
Once the Cloud Function is deployed, you can test it by sending an HTTP request to the function's URL. For example, you can use the following command to send a request to the function:
curl -X POST -H "Content-Type: application/json" -d '{"a": 2, "b": 3}' https://REGION-PROJECT_ID.cloudfunctions.net/calculator
Gemini が提供する gcloud コマンドをターミナルで実行してみましょう。
gcloud functions deploy calculator --runtime ruby32 --trigger-http --entry-point app
次のエラーが発生します。
「ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: Could not find Gemfile.lock file in your app. Please make sure your bundle is up to date before deploying.; Error ID: 7916f05c」
Gemini のサポートを利用してエラーを修正しましょう。
プロンプト:
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: Could not find Gemfile.lock file in your app. Please make sure your bundle is up to date before deploying.; Error ID: 7916f05c
対応:
It looks like you're trying to deploy a function to Cloud Functions, but you don't have a Gemfile.lock file in your app. To fix this, run the following command in your app directory:
bundle install
This will create a Gemfile.lock file that lists all of the gems that your app uses. ...
ターミナルで bundle install を実行して、Gemfile.lock を作成しましょう。コマンドを実行したら、エクスプローラ タブを確認します。Gemfile.lock が表示されているはずです。
Cloud Functions へのデプロイをもう一度試してみましょう。ターミナルで同じデプロイ コマンドを実行します。
gcloud functions deploy calculator --runtime ruby32 --trigger-http --entry-point app
次のエラーが発生します。
「ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: unable to execute functions-framework-ruby; please ensure a recent version of the functions_framework gem is in your Gemfile; Error ID: 89ae7dda」
8. Functions Framework for Ruby
Gemini にエラーについて質問してみましょう。
プロンプト:
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: unable to execute functions-framework-ruby; please ensure a recent version of the functions_framework gem is in your Gemfile; Error ID: 89ae7dda
対応:
It looks like you're trying to deploy a function to Cloud Functions, but the build is failing because the functions_framework gem is not in your Gemfile. To fix this, add the following line to your Gemfile: gem "functions_framework" Then, run the following command to install the gem: bundle install Once the gem is installed, you can try deploying your function again. ....
つまり、アプリに functions_framework gem がありません。Gemfile に次の行を追加しましょう。
gem "functions_framework"
ターミナルで bundle install を実行します。「No explicit global resource in Gemfile」というエラーが表示される場合と表示されない場合があります。このエラーが発生した場合は、Gemfile の 1 行目に次の行があることを確認してください。
source "https://rubygems.org"
その後、ターミナルから bundle install を再度実行します。もう一度デプロイ コマンドを実行します。
gcloud functions deploy calculator --runtime ruby32 --trigger-http --entry-point app --project PROJECT_ID
次のエラーが発生します。
「ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: failed to verify function target "app" in source "app.rb": Undefined function: "app"; Error ID: e12ab0f9」
この問題を解決するために、Gemini の Generate Code 機能を使ってみましょう。[Explorer] タブから app.rb を開きます。ここには、電卓アプリのコードがすでにあります。app.rb の 1 行目で FunctionsFramework と入力し、続けて「.」と入力します。グレーのテキストで候補が表示されます。

Tab キーを押して候補を適用します。承認されると、以下のような候補が表示されます。

Tab キーを押して、さらに提案を受け入れます。app.rb は次のようになります。
(関数名を add から app に変更します)。
require "functions_framework"
FunctionsFramework.http "app" do |request|
a = request.params["a"].to_i
b = request.params["b"].to_i
Calculator.new.add(a, b)
end
class Calculator
def add(a, b)
a + b
end
end
Cloud Functions アプリのエントリ ポイントを作成し、エディタで Gemini のコード候補を使用する方法を学びました。アプリをもう一度デプロイしてみましょう。
gcloud functions deploy calculator --runtime ruby32 --trigger-http --entry-point app --project PROJECT_ID
すべてがうまくいけば、デプロイが完了し、アプリがホストされている URL を確認できます。では、app.rb を少し変更して、加算結果を返すようにしましょう。最終的な app.rb は次のようになります。
require "functions_framework"
FunctionsFramework.http "app" do |request|
a = request.params["a"].to_i
b = request.params["b"].to_i
calculator = Calculator.new
add = calculator.add(a, b)
"Result: #{add}"
end
class Calculator
def add(a, b)
a + b
end
end
9. エンドツーエンド テスト
URL を開き、次のようにクエリ パラメータ a と b を渡します。
https://[APP-URL]/?a=2&b=3
レスポンスは "Result: 5" として表示されます。計算機には多くの操作があります。このアプリを拡張して、これらの処理も実行し、Gemini のサポートを受けることができます。
10. まとめ
今回は、Gemini を使用して TDD アプローチで Ruby アプリを開発する方法について説明しました。ご覧のとおり、Gemini は異なる結果を返すことがありますが、あまり慣れていない言語での開発プロセスを加速させ、学習曲線を加速させる関数やスニペットを表示することはできます。
経験の有無にかかわらず、すべてのデベロッパーが Gemini を開発プロセスに活用できます。開発以外にも、Gemini を使用して既存のコードの説明を取得したり、既存のコードのテストを生成したりできます。これは少なくとも私にとっては最も退屈な作業です。
Gemini を使ってコーディングをお楽しみください。