rspec is a testing DSL. minitest is ruby.
-- Adam Hawkins, "Bow Before MiniTest"
在 DSL 和 Ruby 之间,我更倾向 Pure Ruby,比如 Sinatra 和 Camping 之间我更喜欢 Camping。但是,从生态系统方面,Sinatra 和 RSpec 有更多的用户和生态链。
最近看了一些 Minitest 的文章和视频,我觉得 Minitest 具有以下优点都是我喜欢的:
还有就是我的偶像 tenderlove 和 steveklabnik 都十分推崇 Minitest,Minitest 也是 Rails4 使用的测试框架。
当然,我写测试的经验不是很丰富,可能有遗漏的地方,还请指正。
以下来自Minitest Doc:
require "minitest/autorun"
class TestMeme < Minitest::Test
def setup
@meme = Meme.new
end
def test_that_kitty_can_eat
assert_equal "OHAI!", @meme.i_can_has_cheezburger?
end
def test_that_it_will_not_blend
refute_match /^no/, @meme.will_it_blend?
end
def test_that_will_be_skipped
skip "test this later"
end
end
require "minitest/autorun"
describe Meme do
before do
@meme = Meme.new
end
describe "when asked about cheeseburgers" do
it "must respond positively" do
@meme.i_can_has_cheezburger?.must_equal "OHAI!"
end
end
describe "when asked about blending possibilities" do
it "won't say no" do
@meme.will_it_blend?.wont_match /^no/
end
end
end
http://docs.seattlerb.org/minitest/#label-Benchmarks
class MemeAsker
def initialize(meme)
@meme = meme
end
def ask(question)
method = question.tr(" ","_") + "?"
@meme.__send__(method)
end
end
require "minitest/autorun"
describe MemeAsker do
before do
@meme = MiniTest::Mock.new
@meme_asker = MemeAsker.new @meme
end
describe "#ask" do
describe "when passed an unpunctuated question" do
it "should invoke the appropriate predicate method on the meme" do
@meme.expect :will_it_blend?, :return_value
@meme_asker.ask "will it blend"
@meme.verify
end
end
end
end
http://docs.seattlerb.org/minitest/#label-Stubs