Skip to content

Instantly share code, notes, and snippets.

@pithyless
Created July 23, 2012 12:33
Show Gist options
  • Save pithyless/3163387 to your computer and use it in GitHub Desktop.
Save pithyless/3163387 to your computer and use it in GitHub Desktop.
minitest samples
# https://gist.github.com/2032303
module MiniTest::Assertions
def assert_palindrome(string)
assert string == string.reverse, "Expected #{string} to read the same way backwards and forwards"
end
def assert_default(hash, default_value)
assert default_value == hash.default, "Expected #{default_value} to be default value for hash but was #{hash.default.inspect}"
end
end
String.infect_an_assertion :assert_palindrome, :must_be_palindrome, :only_one_argument
Hash.infect_an_assertion :assert_default, :must_default_to, :do_not_flip
# https://gist.github.com/3113618
MiniTest::Spec.class_eval do
def self.shared_examples
@shared_examples ||= {}
end
end
module MiniTest::Spec::SharedExamples
def shared_examples_for(desc, &block)
MiniTest::Spec.shared_examples[desc] = block
end
def it_behaves_like(desc)
self.instance_eval do
MiniTest::Spec.shared_examples[desc].call
end
end
end
Object.class_eval { include(MiniTest::Spec::SharedExamples) }
# https://gist.github.com/1116486
class MiniTest::Spec
attr_accessor :subject
def self.its msg, &block
it msg.to_s do
subject.send(msg).instance_eval &block
end
end
end
describe CashRegister do
before do
self.subject = CashRegister.new
end
describe "when empty" do
its(:items) { must_be_empty }
its(:total) { must_equal 0 }
end
describe "after scanning 1 and 2" do
before do
subject.scan(1)
subject.scan(2)
end
its(:items) { wont_be_empty }
its(:total) { must_equal 3 }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment