MiniTest::Mock Ruby1.9 标准库支持
发布日期:2021-09-29 20:09:23 浏览次数:7 分类:技术文章

本文共 3686 字,大约阅读时间需要 12 分钟。

[url=http://bfts.rubyforge.org/minitest/MiniTest/Mock.html]官方说明如下[/url]
主要一个expect一个verify
#Expect that method name is called, optionally with args, and returns retval. @mock.expect(:meaning_of_life, 42) @mock.meaning_of_life # => 42 @mock.expect(:do_something_with, true, [some_obj, true]) @mock.do_something_with(some_obj, true) # => true args is compared to the expected args using case equality (ie, the ‘===’ operator), allowing for less specific expectations. @mock.expect(:uses_any_string, true, [String]) @mock.uses_any_string("foo") # => true @mock.verify  # => true @mock.expect(:uses_one_string, true, ["foo"] @mock.uses_one_string("bar") # => true @mock.verify  # => raises MockExpectationError
使用例子如下:
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
给个标准库地址:[url]http://www.ruby-doc.org/stdlib-1.9.3/[/url]
之前的用法:
class AdminMailer     def mail_admins( message )         # ... send emails to all admins     end end class ErrorHandler     def initialize( mailer )         @mailer = mailer     end     def execute         begin             yield         rescue RuntimeError => err             @mailer.mail_admins( err.message )         end     end end require 'test/unit' require 'test/unit/mock' class ErrorHandlerTestCase < Test::Unit::TestCase     def test_error_should_mail_all_admins_with_error_message         mock_mailer = Test::Unit::MockObject( AdminMailer ).new         mock_mailer.set_call_order( :mail_admins )         mock_mailer.activate         handler = ErrorHandler.new( mock_mailer )         assert_nothing_raised do             handler.execute {
raise "Something bad happened" } end mock_mailer.verify end end
assert( boolean, [message] )	#True if boolean assert_equal( expected, actual, [message] ) assert_not_equal( expected, actual, [message] )	#True if expected == actual assert_match( pattern, string, [message] ) assert_no_match( pattern, string, [message] )	#True if string =~ pattern assert_nil( object, [message] ) assert_not_nil( object, [message] )	#True if object == nil assert_in_delta( expected_float, actual_float, delta, [message] )	#True if (actual_float - expected_float).abs <= delta assert_instance_of( class, object, [message] )	#True if object.class == class assert_kind_of( class, object, [message] )	#True if object.kind_of?(class) assert_same( expected, actual, [message]) assert_not_same( expected, actual, [message] )	#True if actual.equal?( expected ). assert_raise( Exception,... ) {block} assert_nothing_raised( Exception,...) {block}	#True if the block raises (or doesn't) one of the listed exceptions. assert_throws( expected_symbol, [message] ) {block} assert_nothing_thrown( [message] ) {block}	#True if the block throws (or doesn't) the expected_symbol. assert_respond_to( object, method, [message] )	#True if the object can respond to the given method. assert_send( send_array, [message] )	#True if the method sent to the object with the given arguments return true. assert_operator( object1, operator, object2, [message] )	#Compares the two objects with the given operator, passes if true

转载地址:https://blog.csdn.net/horace_lee/article/details/84085778 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:MiniTest::Mock 二,进一步示例分析
下一篇:Ruby 1.9 Rspec 风格 Unit Test MiniTest::Spec介绍

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月04日 23时54分14秒