Minitest Cheat Sheet



Minitest cheat sheet pdf

The MiniTest Assertions Cheat Sheet was released by CITguy on Cheatography. Here's how they described it: Download the PDF version here View Entire Discussion (1 Comments). Require 'minitest/autorun' # 注意有些资料中,测试类不是继承自MiniTest::Test, # 那是MiniTest 5之前的做法,MiniTest会通知你改正 class TestMyLife MiniTest:: Test # 这个方法会在各个测试之前被调用 def setup @me = People. New end def testsleep # assertequal exp, act, msg assertequal 'zzZ', @me.sleep.

Minitest Cheat Sheet Excel

Ruby’s standard testing suite, MiniTest, is in dire need of a quick-and-handy reference for its syntax. I’ve put one together comparing the unit syntax (assert/refute) and the spec syntax (must/wont). You can see it below or download the Google Spreadsheet I made and roll your own sheet (HTML, XLS)

Test syntax

UnitSpecArgumentsExamples

assert_empty
refute_empty

must_be_empty
wont_be_empty

obj, msg=nil

assert_empty []
refute_empty [1,2,3]
[].must_be_empty
[1,2,3].wont_be_empty


assert_equal
refute_equal

must_equal
wont_equal

exp, act, msg=nil

assert_equal 2, 2
refute_equal 2,1
2.must_equal 2
2.wont_equal 1


assert_in_delta
refute_in_delta

must_be_within_delta
wont_be_within_delta

exp, act, dlt=0.001, msg=nil

assert_in_delta 2012, 2010, 2
refute_in_delta 2012, 3012, 2
2012.must_be_within_delta 2010, 2
2012.wont_be_within_delta 3012, 2


must_be_close_to
wont_be_close_to

act, dlt=0.001, msg=nil

2012.must_be_close_to 2010, 2
2012.wont_be_close_to 3012, 2


assert_in_epsilon
refute_in_epsilon

must_be_within_epsilon
wont_be_within_epsilon

a, b, eps=0.001, msg=nil

assert_in_epsilon 1.0, 1.02, 0.05
refute_in_epsilon 1.0, 1.06, 0.05
1.0.must_be_within_epsilon 1.02, 0.05
1.0.wont_be_within_epsilon 1.06, 0.05


assert_includes
refute_includes

must_include
wont_include

collection, obj, msg=nil

assert_includes [1, 2], 2
refute_includes [1, 2], 3
[1, 2].must_include 2
[1, 2].wont_include 3


assert_instance_of
refute_instance_of

must_be_instance_of
wont_be_instance_of

klass, obj, msg=nil

assert_instance_of String, 'bar'
refute_instance_of String, 100
'bar'.must_be_instance_of String
100.wont_be_instance_of String


assert_kind_of
refute_kind_of

must_be_kind_of
wont_be_kind_of

klass, obj, msg=nil

assert_kind_of Numeric, 100
refute_kind_of Numeric, 'bar'
100.must_be_kind_of Numeric
'bar'.wont_be_kind_of Numeric


assert_match
refute_match

must_match
wont_match

exp, act, msg=nil

assert_match /d/, '42'
refute_match /d/, 'foo'
'42'.must_match /d/
'foo'.wont_match /d/


assert_nil
refute_nil

must_be_nil
wont_be_nil

obj, msg=nil

assert_nil [].first
refute_nil [1].first
[].first.must_be_nil
[1].first.wont_be_nil


assert_operator
refute_operator

must_be
wont_be

o1, op, o2, msg=nil

assert_operator 1, :<, 2
refute_operator 1, :>, 2
1.must_be :<, 2
1.wont_be :>, 2


assert_output

must_output

stdout = nil, stderr = nil

assert_output('hin'){ puts 'hi' }
Proc.new{puts 'hi'}.must_output 'hin'


assert_raises

must_raise

*exp

assert_raises(NoMethodError){ nil! }
Proc.new{nil!}.must_raise NoMethodError


assert_respond_to
refute_respond_to

must_respond_to
wont_respond_to

obj, meth, msg=nil

assert_respond_to 'foo',:empty?
refute_respond_to 100, :empty?
'foo'.must_respond_to :empty?
100.wont_respond_to :empty?


assert_same
refute_same

must_be_same_as
wont_be_same_as

exp, act, msg=nil

assert_same :foo, :foo
refute_same ['foo'], ['foo']
:foo.must_be_same_as :foo
['foo'].wont_be_same_as ['foo']


assert_silent

must_be_silent

-

assert_silent{ 1 + 1 }
Proc.new{ 1 + 1}.must_be_silent


assert_throws

must_throw

sym, msg=nil

assert_throws(:up){ throw :up}
Proc.new{throw :up}.must_throw :up

Test Setup

UnitSpec
setup()before(type = nil, &block)
teardown()after(type = nil, &block)

Mocks

Minitest Cheat Sheet Pdf

via MiniTest::Mock

Minitest Cheat Sheet Printable

  • expect(name, retval, args=[]) – Expect that method name is called, optionally with args or a blk, and returns retval.

    Example:

  • verify – Verify that all methods were called as expected. Raises MockExpectationError if the mock object was not called as expected.

Other syntax

Minitest
  • def flunk(msg=nil)
  • def pass(msg=nil)
  • def skip(msg=nil, bt=caller)
  • def it (desc='anonymous', &block)
  • i_suck_and_my_tests_are_order_dependent!() – Call this at the top of your tests when you absolutely positively need to have ordered tests. In doing so, you’re admitting that you suck and your tests are weak. (TestCase public class method)
  • parallelize_me!() – Call this at the top of your tests when you want to run your tests in parallel. In doing so, you’re admitting that you rule and your tests are awesome.
  • make_my_diffs_pretty!() – Make diffs for this TestCase use pretty_inspect so that diff in assert_equal can be more details. NOTE: this is much slower than the regular inspect but much more usable for complex objects.

Tutorials

Reference

  • My Google Spreadsheet of MiniTest calls, as Excel
  • StackOverflow: How do I mock things in MiniTest?
Minitest

Brought to you by

Welcome. Free Books in the Yuki & Moto Press Series about Ruby 'n' Friends include:

Gem Developer's Guide by , , et al (Source in the Manuscripts book format)

How I Start - Let's Build a Gem Together! by (Source in the Manuscripts book format)

Hoe Developer's Guide - Build, Package and Publish Gems with Rake Tasks - Ready-to-Use Build Scripts by , et al (Source in the Manuscripts book format)

Minitest Cheat Sheet

Sinatra Intro by , , et al (Source in the Manuscripts book format)

Best of Practicing Ruby by , , , , , et al (Source in the Manuscripts book format)

Programming Blockchains Step-by-Step from Scratch (Zero). Starting with Crypto Hashes... by , et al (Source in the Manuscripts book format)

Gem Series ++ Programming Cryptocurrencies and Blockchains by , et al (Source in the Manuscripts book format)

Gem Series ++ Project Automation & Database Documentation Tools by , et al (Source in the Manuscripts book format)

Minitest Cheat Sheet Template

Gem Series ++ Web Services (HTTP JSON APIs) the Modern Micro Way by , et al (Source in the Manuscripts book format)

Net::HTTP by Example / Net::HTTP Cheat Sheet by , et al (Source in the Manuscripts book format)

FizzBuzz (1, 2, Fizz, 4, Buzz,...) by Example - There's More Than One Way To Do It by , , et al (Source in the Manuscripts book format)

Ruby Best Practices by (Source in the Manuscripts book format)

More Bookshelves

Bits & Blocks Press • Hyde Press • Fun Press • World Classics

Built with Octobook (powered by Jekyll) on 2018-05-11 08:33:54 +0000 in 2.371 seconds.
Hosted on GitHub Pages. </> Source on GitHub. (0) Dedicated to the public domain.