Skip to content

ai_rspec_writer is a CLI tool that automatically generates RSpec test cases for Rails applications using ChatGPT and Gemini AI. It simplifies test writing for models, controllers, and services, ensuring 100% test coverage with minimal effort.

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.txt
Notifications You must be signed in to change notification settings

aniruddhami/ai_rspec_writer

ai_rspec_writer

ai_rspec_writer is an AI-powered tool for generating RSpec test cases in Ruby on Rails applications. It supports ChatGPT and Gemini AI to create model, controller, and service tests automatically, improving test coverage and development efficiency.


πŸš€ Features

  • πŸ“ Automatically generates RSpec tests for models, controllers, and services.
  • πŸ“Œ Extracts database schema for validation and association tests.
  • πŸ›  Supports both ChatGPT and Gemini AI.
  • πŸ” Customizable test instructions using extra comments.
  • πŸ† Ensures high code coverage by generating edge-case scenarios.
  • πŸ”‘ Secure API authentication using OpenAI & Google Gemini keys.

πŸ›  Installation

1️⃣ Add to your Gemfile

group :development, :test do
  gem 'ai_rspec_writer'
  gem 'factory_bot_rails' # Generates dummy data
  gem 'rails-controller-testing' # Required for controller specs
  gem 'faker' # Fake data generation
  gem 'shoulda-matchers', '~> 5.0' # One-liner matchers for RSpec
  gem 'simplecov', require: false # Code coverage analysis
end

2️⃣ Run Bundle Install

bundle install

3️⃣ Set up API Keys

Create a .env file in your project root and add:

CHATGPT_API_KEY=your-openai-api-key
GEMINI_API_KEY=your-google-gemini-api-key
DEFAULT_AI=chatgpt
CHATGPT_MODEL=your-model
GEMINI_MODEL=your-model

Or export them in your terminal:

export CHATGPT_API_KEY="your-openai-api-key"
export GEMINI_API_KEY="your-google-gemini-api-key"
export DEFAULT_AI="chatgpt"
export CHATGPT_MODEL="your-model"
export GEMINI_MODEL="your-model"
Default Setting:
GEMINI_MODEL=o3-mini
CHATGPT_MODEL=gemini-2.0-flash
DEFAULT_AI=chatgpt

πŸ“Œ Usage

Command Structure

bundle exec ai_rspec_writer -f <file_path> -t <table_names> -e "<extra_comment>" -a <ai_model>

πŸ”Ή Example Commands

βœ… Generate RSpec tests using Gemini AI

bundle exec ai_rspec_writer -f app/controllers/obento/obento_zaikos_controller.rb,app/models/store_notification.rb,app/models/common_setting.rb -t store_notifications,common_settings -e "use devise admin and ensure 100% test coverage" -a gemini

βœ… Generate RSpec tests using ChatGPT AI

bundle exec ai_rspec_writer -f app/controllers/obento/obento_zaikos_controller.rb,app/models/store_notification.rb,app/models/common_setting.rb -t store_notifications,common_settings   -e "use devise admin and ensure 100% test coverage" -a chatgpt

βœ… Generate RSpec tests without -a flag

If no AI model (-a) is passed, ai_rspec_writer will default to DEFAULT_AI from .env or use ChatGPT if not explicitly set.

bundle exec ai_rspec_writer -f app/controllers/obento/obento_zaikos_controller.rb,app/models/store_notification.rb,app/models/common_setting.rb -t store_notifications,common_settings   -e "use devise admin and ensure 100% test coverage"

πŸ” Command Options

Option Description Example
-f, --files Files to generate tests for (comma-separated paths). -f app/models/user.rb,app/controllers/users_controller.rb
-t, --table_name Database tables to extract schema info for tests. -t users,orders
-e, --ec Extra comments to customize test behavior. -e "use Devise authentication"
-a, --ai AI model to use (chatgpt or gemini). -a gemini

πŸ“ Example Output

After running:

bundle exec ai_rspec_writer -f app/models/store_notification.rb -t store_notifications -e "use FactoryBot"

It generates:

# spec/models/store_notification_spec.rb
require 'rails_helper'

RSpec.describe StoreNotification, type: :model do
  let(:store_notification) { create(:store_notification) }

  describe "Validations" do
    it { should validate_presence_of(:title) }
    it { should validate_presence_of(:message) }
  end

  describe "Associations" do
    it { should belong_to(:store) }
  end

  describe "Methods" do
    it "sends notification successfully" do
      expect(store_notification.send_notification).to be_truthy
    end
  end
end

⚠️ Troubleshooting

❌ ChatGPT API key is missing.

Solution: Set the API key in .env:

CHATGPT_API_KEY=your-api-key

Or export it:

export CHATGPT_API_KEY="your-api-key"

❌ Gemini API key is missing.

Solution: Set the Gemini API key in .env:

GEMINI_API_KEY=your-api-key

❌ Error: Please provide a file to generate tests for using -f

Solution: Ensure you pass the -f option with valid file paths:

bundle exec ai_rspec_writer -f app/models/user.rb

❌ Tests are not covering all cases

Solution: Try providing additional extra comments:

bundle exec ai_rspec_writer -f app/models/user.rb -e "Generate edge case tests"

πŸ“œ License

This project is licensed under the MIT License.


πŸ‘¨β€πŸ’» Contributors

  • @aniruddhami πŸŽ‰
  • Open for Contributions! Feel free to fork & improve.

✨ Future Improvements

  • πŸ“Œ RSpec test generation improvements
  • πŸ“Œ Support for Minitest
  • πŸ“Œ AI model selection enhancements
  • πŸ“Œ More detailed error handling

About

ai_rspec_writer is a CLI tool that automatically generates RSpec test cases for Rails applications using ChatGPT and Gemini AI. It simplifies test writing for models, controllers, and services, ensuring 100% test coverage with minimal effort.

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.txt

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •