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] をクリックします。
- 重要: こちらの Codelab のステップ 1 と 2 に沿って Gemini の使用を開始し、Cloud Shell IDE の Gemini をそれぞれ有効にします。
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 を使用します。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 Editor が開きます。
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 の概要とその利点について簡単に説明します。
では、Ruby とは何かを Gemini に尋ねましょう。
プロンプト:
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 に求めます。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. .....
Calculator
というクラスはなく、作成する必要があるというメッセージが Gemini から届いています。また、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"
Calculator クラスが定義されている app.rb をインポートするためのレスポンスを Gemini から受け取りました。適切な回答を得るまでにプロンプトを 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 にデプロイする
では、これを Cloud Functions の関数にデプロイするよう Gemini に依頼します。
プロンプト:
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 を使用して手動でデプロイする方法を説明します。でも私は考えたくない。統合ターミナルから直接実行できる gcloud
コマンドを Gemini にリクエストしてみましょう。
プロンプト:
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
を実行します。「Gemfile に明示的なグローバル リソースがありません」というエラーが表示される場合と表示されない場合がありますをタップします。この問題が発生した場合は、Gemfile の最初の行に次の行があることを確認してください。
source "https://rubygems.org"
次に、ターミナルから bundle install
を再度実行します。ここで、deploy コマンドを再度実行します。
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
機能を試してみましょう。電卓アプリのコードがすでに用意されている [エクスプローラ] タブから app.rb を開きましょう。app.rb の最初の行に「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 をよろしくお願いいたします。