Unit testing
Unit testing refers to when you want to test a specific method, whether it be on the top level or as part of an object, in isolation. Testing it in isolation is an important part of this type of testing. Doing this ensures that you are only testing the logic you want and not the logic of its dependencies.
Crystal comes bundled with the Spec
module, which provides the tools required to test your code. For example, say you have the following method that returns the sum of two values as part of add.cr
:
def add(value1, value2) value1 + value2 end
The related tests for this could look like this:
require "spec" require "./add" describe "#add" do it "adds with positive values" do add(1, 2).should eq 3 end it "adds with negative values" do add(-1, -2).should eq -3 end it "adds with mixed...