Testing and Versioning
Quality and semver.
Testing and Versioning is a free Ruby Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Test a Gem?
Because a gem is used by many projects, a bug affects everyone. Automated tests let you change code with confidence and prove the gem still works.
- Tests document expected behavior.
- They catch regressions before release.
- They are required by serious users before they adopt a gem.
Choosing a Framework
The two popular choices are Minitest (ships with Ruby, lightweight) and RSpec (rich DSL, very common). Either is fine; pick one and add it as a development dependency in the gemspec.
A Minitest Example
Minitest tests are plain classes with assertion methods. Here a self-contained example that runs your gem's logic and checks the result.
require "minitest/autorun"
def greet(name)
"Hi, " + name
end
class GreetTest < Minitest::Test
def test_greet
assert_equal "Hi, Sam", greet("Sam")
end
endCommon Assertions
Useful Minitest assertions:
assert_equal expected, actualassert objandrefute objassert_nil valueassert_raises(Error) { ... }
Each test should check one clear behavior.
require "minitest/autorun"
class MathTest < Minitest::Test
def test_addition
assert_equal 4, 2 + 2
end
def test_truth
refute_nil 5
end
endRunning and Automating Tests
You usually add a Rake task so rake test runs the whole suite. Continuous integration then runs the same task on every push, on several Ruby versions, to catch problems early.
What Is Semantic Versioning?
Semantic Versioning (semver) gives meaning to a version number MAJOR.MINOR.PATCH:
- MAJOR: incompatible API changes.
- MINOR: new features, backward compatible.
- PATCH: backward compatible bug fixes.
Users rely on this to upgrade safely.
Reading a Version
Given 2.4.1: major is 2, minor is 4, patch is 1. Splitting the string shows the parts.
version = "2.4.1"
major, minor, patch = version.split(".")
puts "Major: " + major
puts "Minor: " + minor
puts "Patch: " + patchWhen to Bump Each Part
Decision rules:
- Fixed a bug without changing the API? Bump patch.
- Added a feature, old code still works? Bump minor, reset patch to 0.
- Removed or changed existing behavior? Bump major, reset minor and patch to 0.
def bump_minor(version)
major, minor, _patch = version.split(".").map(&:to_i)
major.to_s + "." + (minor + 1).to_s + ".0"
end
puts bump_minor("1.3.5")Version Constraints
Users pin your gem with operators in their Gemfile:
'~> 1.4'allows 1.4 up to but not including 2.0 (pessimistic).'>= 1.4'allows any newer version.'= 1.4.2'locks an exact version.
The pessimistic ~> is the most common, balancing updates and safety.
Keep a Changelog
Maintain a CHANGELOG.md listing what changed in each version. It tells users what is new, what broke, and how to migrate. Pair every version bump with a changelog entry.
Pre-Release Versions
Versions like 2.0.0.beta1 mark unstable previews. Users must opt in explicitly to install them, so you can ship early versions for testing without affecting normal installs.
version = "2.0.0.beta1"
puts "Pre-release? " + version.include?("beta").to_sQuick Check
Test your understanding of versioning.
Recap: Testing and Versioning
You learned how to keep a gem reliable:
- Write tests with Minitest or RSpec; automate with Rake and CI.
- Follow semver: MAJOR.MINOR.PATCH.
- Bump the right part for fixes, features, or breaking changes.
- Use
~>constraints and keep a changelog.
Next we publish the gem to RubyGems.
Frequently asked questions
Is the “Testing and Versioning” lesson free?
Yes — the full text of “Testing and Versioning” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Testing and Versioning”?
Quality and semver. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Testing and Versioning” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Gem Structure
- Writing the Code
- Testing and Versioning
- Publishing to RubyGems