The word its
A method and its expectation on one line, with the fix-its gem.
Many contexts hold one method and one expectation:
require "fix"
Fix :Team do
on :size do
it MUST eq 3
end
on :first do
it MUST eq "Ada"
end
end
exit(Fix[:Team].test { %w[Ada Grace Linus] })
With fix-its, each of them takes one line. Install it, gem install fix-its or gem "fix-its" in a Gemfile, and require fix/its, which requires fix:
require "fix/its"
Fix :Team do
its(:size) { MUST eq 3 }
its(:first) { MUST eq "Ada" }
its(:include?, "Linus") { MUST be_true }
its(:join, ", ") { MUST eq "Ada, Grace, Linus" }
on :last do
its(:length) { MUST eq 5 }
end
end
exit(Fix[:Team].test { %w[Ada Grace Linus] })
ruby team_fix.rb team_fix.rb:4: Success: expected 3 to eq 3 team_fix.rb:5: Success: expected "Ada" to eq "Ada" team_fix.rb:6: Success: expected true to be true team_fix.rb:7: Success: expected "Ada, Grace, Linus" to eq "Ada, Grace, Linus" team_fix.rb:10: Success: expected 5 to eq 5
its(:size) { MUST eq 3 } is on(:size) { it MUST eq 3 }, and the line points at the its.
itstakes the name of the method and its arguments, asondoes:its(:join, ", ").- Its block gives the requirement, a level and a matcher, and is read with the rest of the document. A block that gives anything else, or no block, raises
ArgumentErrorwhen the file is loaded. - Inside an
onor awith,itssends its method to the value of that context: above, the length of the last name. itsis a word of the language, so a let cannot take that name.
Since Ruby 3.4, a bare it in a block is its parameter: in its(:size) { MUST eq it }, it is nil. On Ruby 3.3, it is the word of Fix, which raises ArgumentError for want of a requirement. Write the value.
Use its for a method and one expectation; keep on for a context with several expectations, a with, or a further on inside it.