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 페이지를 방문하여 '채팅 시작'을 클릭합니다.
- 중요: 이 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 활성화
- 필요한 언어가 설치되어 미리 로드되어 제공되는 Google Cloud 콘솔에서 실행되는 명령줄 환경인 Cloud Shell을 사용합니다. 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. .....
계산기 앱(예: 두 숫자를 더하는 경우)의 샘플 테스트 사례를 제공합니다. 또한 테스트에 필요한 종속 항목 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"
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에 앱 배포
이제 Gemini에게 Cloud 함수에 배포해 달라고 요청해 보겠습니다.
프롬프트:
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. 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를 다시 실행합니다. 이제 배포 명령어를 다시 실행해 보겠습니다.
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로 즐거운 코딩을 하세요.