-
-
Save steveklabnik/5475159 to your computer and use it in GitHub Desktop.
| require 'test_helper' | |
| describe "foo" do | |
| include SupportsCount | |
| end |
| module SupportsCount | |
| describe "#count" do | |
| it "is a dummy test for this example" do | |
| assert true | |
| end | |
| end | |
| end |
It's so simple. In this case synthetic language wins over "English".
@steveklabnik this won't work for a more complicated example because it's immediately evaluated, eg.:
module SupportsCount
describe "#count" do
it "returns a natural number" do
assert_operator @object.count, :>=, 0
end
end
endWe want to evaluate this only after it's included, like this:
module SupportsCount
def self.included(base)
describe "#count" do
end
end
endThere might be a way to write custom Module#describe so that we keep the DSL minimal, but I can't figure it out. Maybe defining just Module#it would be a better DSL anyway?
/cc @zenspider
require 'minitest/autorun'
class Module
def it(*args, &block)
self.class.send(:define_method, :included) do |base|
base.it(*args, &block)
end
end
end
module SupportsCount
it "returns a natural number" do
assert_operator @object.count, :>=, 0
end
end
describe Array do
include SupportsCount
before do
@object = []
end
endFor only one it per module? I don't see the point (even if it supports more than one).
Fixed version: https://gist.github.com/wojtekmach/5583628
@zenspider you probably already know that, but I was actually surprised that getting 95% use cases of shared examples with spec DSL is as simple as:
class Module
include Minitest::Spec::DSL
end
module SupportsCount
it 'responds to count' do
end
endit, before, after just works. It's kind of beautiful it's so simple. describe doesn't work but who cares.
@steveklabnik sorry for bombing your gist :p
<3