Created
December 10, 2012 02:18
-
-
Save rastasheep/4248006 to your computer and use it in GitHub Desktop.
Revisions
-
rastasheep created this gist
Dec 10, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,144 @@ ## MiniTest::Spec use __must__ for positive expectations and __wont__ for negative expectations. * __must_be__ | list.size.must_be :==, 0 * __must_be_close_to__ | subject.size.must_be_close_to 1,1 * __must_be_empty__ | list.must_be_empty * __must_be_instance_of__ | list.must_be_instance_of Array * __must_be_kind_of__ | list.must_be_kind_of Enumerable * __must_be_nil__ | list.first.must_be_nil * __must_be_same_as__ | subject.must_be_same_as subject * __must_be_silent__ | proc { "no stdout or stderr" }.must_be_silent * __must_be_within_epsilon__ | subject.size.must_be_within_epsilon 1,1 * __must_equal__ | subject.size.must_equal 2 * __must_include__ | subject.must_include "skinny jeans" * __must_match__ | subject.first.must_match /silly/ * __must_output proc__ |{ print "#{subject.size}!" }.must_output "2!" * __must_respond_to__ | subject.must_respond_to :count * __must_raise__ | proc { subject.foo }.must_raise NoMethodError * __must_send__ | subject.must_send [subject, :values_at, 0] * __must_throw__ | proc { throw :done if subject.any? }.must_throw :done ```ruby require 'minitest/autorun' describe Hipster, "Demonstration of MiniTest" do before do @hipster = Hipster.new end after do @hipster.destroy! end subject do Array.new.tap do |attributes| attributes << "silly hats" attributes << "skinny jeans" end end let(:list) { Array.new } describe "when asked about the font" do it "should be helvetica" do @hipster.preferred_font.must_equal "helvetica" end end describe "when asked about mainstream" do it "won't be mainstream" do @hipster.mainstream?.wont_equal true end end end ``` ## MiniTest::Unit::TestCase use __assert__ for positive assertions and __refute__ for negative assertions * __assert__ | assert @subject.any?, "empty subjects" * __assert_block__ | assert_block { @subject.any? } * __assert_empty__ | assert_empty @list * __assert_equal__ | assert_equal 2, @subject.size * __assert_in_delta__ | assert_in_delta @subject.size, 1,1 * __assert_in_epsilon__ | assert_in_epsilon @subject.size, 1, 1 * __assert_includes__ | assert_includes @subject, "skinny jeans" * __assert_instance_of__ | assert_instance_of Array, @list * __assert_kind_of__ | assert_kind_of Enumerable, @list * __assert_match__ | assert_match @subject.first, /silly/ * __assert_nil__ | assert_nil @list.first * __assert_operator__ | assert_operator @list.size, :== , 0 * __assert_output__ | assert_output("Size: 2") { print "Size: #{@subject.size}"} * __assert_raises__ | assert_raises(NoMethodError) { @subject.foo } * __assert_respond_to__ | assert_respond_to @subject, :count * __assert_same__ | assert_same @subject, @subject, "It's the same object silly" * __assert_send__ | assert_send [@subject, :values_at, 0] * __assert_silent__ | assert_silent { "no stdout or stderr" } * __assert_throws__ | assert_throws(:error,'is empty') {throw :error if @subject.any?} ```ruby require 'minitest/autorun' class TestHipster < MiniTest::Unit::TestCase def setup @hipster = Hipster.new @list = Array.new @subject = ["silly hats", "skinny jeans"] end def teardown @hipster.destroy! end def test_for_helvetica_font assert_equal "helvetica!", @hipster.preferred_font end def test_not_mainstream refute @hipster.mainstream? end end ``` ## MiniTest::Mock there two essential methods at our disposal: __expect__ and __verify__ ```ruby require 'minitest/autorun' class Twipster def initialize(twitter) # A Ruby wrapper for the Twitter API @twitter = twitter end def submit(tweet) @twitter.update("#{tweet} #lolhipster") end end describe Twipster, "Make every tweet a hipster tweet." do before do @twitter = MiniTest::Mock.new @twipster = Twipster.new(@twitter) end it "should append a #lolhipster hashtag and update Twitter with our status" do tweet = "Skyrim? Too mainstream." @twitter.expect :update, true, ["#{tweet} #lolhipster"] @twipster.submit(tweet) assert @twitter.verify # verifies tweet and hashtag was passed to `@twitter.update` end end ``` ___ ###### Credits This is slightly __modified__ version of [@mattsears](https://twitter.com/mattsears)'s article <http://mattsears.com/articles/2011/12/10/minitest-quick-reference/> just in case,if some servers stop working :)