Ad space (banner)
๐Ÿ’ŽRuby Lessons
Lesson 28 / 68

Testing with Your Own assert Method

In this lesson you will learn to write your own assert method and pick up the basic idea behind testing. This is for anyone searching for "Ruby testing how to write".

Ruby has no built-in assert, but you can write a small checking method yourself. Checking automatically that a function returns the expected result is the basic idea behind test code.

The sample code defines its own assert_equal method that compares the expected value with the actual one and prints the result. It passes the result of add(2, 3) in to confirm the method works.

A common stumbling block for beginners is that in practice you would use a dedicated framework such as RSpec. The underlying idea is exactly what you learn here, and building the mechanism yourself first deepens the understanding.

Get into the habit here of comparing "the expected result" with "the actual result". Doing that for every method you write makes later changes and refactoring far less nerve-racking.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Ruby directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)