I was pretty shocked that RSpec demonstration only scratched the surface.
RSpec is to me far superior to C# tests that come out of visual studio by default because of these 3 things:
1) RSpec has ability to format tests as documentation and print it as such.
This is snipped example of such output (for specifications table class Specs in module Specs)
To get documentation for a file_spec.rb you'd type
rspec --format documentation file_spec.rb
or short -f d
rspec -f d file_spec.rb
This is output
Specs::ConvertableProperty
behaves like a validated property
.type_valid?
returns true for all known types
returns false for unknown type
#allowed_units
returns array when allowed_units for that type is an array
for type :weight returns ["lbs", "kg"]
for type :torque returns ["ft-lb", "Nm"]
#unit_valid?
when allowed units are given as array
for type :weight
returns true for
"lbs"
"kg"
returns false for
unit not listed in allowed_units[:weight]
:weight unit is "lb" (missing last letter)
:weight unit is "LBS" (wrong case)
:weight unit is "k" (missing last letter)
:weight unit is "KG" (wrong case)
for type :torque
returns true for
"ft-lb"
"Nm"
returns false for
unit not listed in allowed_units[:torque]
:torque unit is "ft-l" (missing last letter)
:torque unit is "FT-LB" (wrong case)
:torque unit is "N" (missing last letter)
:torque unit is "nM" (wrong case)
...
When you are reading tests for code you didn't write it's much better to start with an overview like this and read into test code if you want to.
2) Specs (tests) can be created dynamically
3) Specs (tests) can be reused
Here is example that explains both. I have added comments for non ruby programmers # this is a comment
For non ruby - rspec users:
describe or context is a nested block of tests
it is a test
shared_examples_for 'close looking strings' do |unit, type|
subject { tested_class.new(type) }
missing_letter_unit = unit[0..-2]
it "#{type.inspect} unit is #{missing_letter_unit.inspect} \
(missing last letter)" do
expect(subject.unit_valid?(missing_letter_unit)).to be false
end
if wrong_case = unit.dup.swapcase!
it "#{type.inspect} unit is #{wrong_case.inspect} (wrong case)" do
expect(subject.unit_valid?(wrong_case)).to be false
end
end
end
This is generating test cases for missing last letter and wrong case. I am using them like this:
context 'returns false for' do
# irrelevant code snipped
units.each do |unit|
include_examples 'close looking strings', unit, type
end
end
Result is that test cases for missing last letter and wrong case are auto generated for each type
This is documentation output for the relevant part.
for type :weight
returns true for
"lbs"
"kg"
returns false for
unit not listed in allowed_units[:weight]
:weight unit is "lb" (missing last letter)
:weight unit is "LBS" (wrong case)
:weight unit is "k" (missing last letter)
:weight unit is "KG" (wrong case)
Is everything clear or do I need to explain something further?
I think C# / Java test tools don't have any of these features. Am I correct?