
Show HN: Fix 1.0 – Ruby tests written as RFC 2119 specifications
Fix 1.0 is out. Fix is a testing framework for Ruby in which a test suite is a specification: a document that says what a subject MUST, SHOULD or MAY do, in the words of RFC 2119, and nothing about how the subject is ma…
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
Fix 1.0 is out. Fix is a testing framework for Ruby in which a test suite is a specification: a document that says what a subject MUST, SHOULD or MAY do, in the words of RFC 2119, and nothing about how the subject is made. I started it in Osaka in 2014 and have reworked it many times since; this is the first version I consider finished.
Ruby knows a duck by what it does: that is duck typing. A specification of Fix does the same: it names no class, and the test, at the end of the file, picks the subject. Here, a duck and a rubber duck, against one specification:
require "fix"
Fix :Duck do on :quack do it MUST eq "Quack!" end
on :swim do it SHOULD eq "Splash!" end
on :fly do it MAY be_true end end
class Duck def quack = "Quack!" def swim = "Splash!" def fly = true end
class RubberDuck def quack = "Quack!" def swim = "Splash!" end
exit([ Fix[:Duck].test { Duck.new }, Fix[:Duck].test { RubberDuck.new } ].all?)
ruby duck_fix.rb duck_fix.rb:5: Success: expected "Quack!" to eq "Quack!" duck_fix.rb:9: Success: expected "Splash!" to eq "Splash!" duck_fix.rb:13: Success: expected true to be true duck_fix.rb:5: Success: expected "Quack!" to eq "Quack!" duck_fix.rb:9: Success: expected "Splash!" to eq "Splash!" duck_fix.rb:13: Info: NoMethodError: undefined method 'fly' for an instance of RubberDuck
The rubber duck cannot fly, but flying was only a MAY: its line is an info, and both ducks meet the specification. Three levels tell what is required, what is recommended and what is optional, and the lines say exactly what happened, one per expectation; the status of the process says whether the whole is met. The same specification can test a class of yours, a library you depend on, or the next version of either.
gem install fix-command
Then the guide: a first specification, the language, the matchers one by one, the command, and what changes when you come from RSpec. Every example on its pages is run before they are published. The README is the reference. Fix needs Ruby 3.3 or later and fork(2).
Write a specification for something you have, and see what it says. That is what Fix is for.
Filed under framework and release. All posts
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。