TEST AUTOMATION: SIMPLE CALCULATOR
OVERVIEW
I created a simple calculator using Ruby and tested the functionality of the object using Rspec. Here is a link to the public github of the project containing the repository and all files needed to run the test here.
More details will be added to the README.md file.
Here is the process of how I created the calculator and rspec
*
*
*
Created a class file to contain the calculator.
Wrote several methods for all the basic operations a calculator should have: sum, difference, product, and quotient.
Next, created the Rspec tests after running in the CLI.
rspec --init
This will create the spec_helper.rb file and spec folder to store the tests needed for the calculator.
đź’ˇREVIEW // TAKEAWAYS
One issue I kept encountering was the load errors when Rspec directories were not properly configured to read. Even though the test files were listed under spec, each time returned a load error.
I was able to figure out how to solve this when I debugged the error logs, determining that the directory was not at fault, but rather creating a new file through the CLI was throwing a UTC error that did not allow Rspec to parse the correct test information.
Overcoming this by creating a new file instead of using
echo nul > calc_spec.rb
This would cause a nul .rb file to be created with an unidentified UTC character that couldn't be located.
--
Another pitfall that I came fell into was during the ZeroDivisionError message. The test required matching the expect clause to be: to raise_error(ZeroDivisionError) each time the quotient function was used. Except, it continuously failed. Instead of removing the test, I was able to figure out a way to catch the denominator by zero error in the class method to return a string text.
In the Rspec test, when the 0 value was used, I would just require the 'Cannot divide by zero!' string text to be the value matched in eql('').Â
The issue was, the quotient method could only output one value, either the error or the string text to catch user input. As Rspec expect statement allows only one value to be matched.