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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年04月04日 23时54分14秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
千万级并发HAproxy均衡负载系统介绍
2019-04-27
什么是A记录、MX记录、CNAME记录
2019-04-27
MongoDB简介
2019-04-27
Varnish purges 缓存清除
2019-04-27
Linux下redis安装部署
2019-04-27
水平切分与垂直切分
2019-04-27
MySQL引擎
2019-04-27
MySQL下的NoSQL解决方案HandlerSocket
2019-04-27
Apache服务器下使用 ab 命令进行压力测试
2019-04-27
查看Firefox中的缓存
2019-04-27
http header头设置反向代理不缓存
2019-04-27
配置MySQL主从复制
2019-04-27
CI框架如何删除地址栏的 index.php
2019-04-27
expires与etag控制页面缓存的优先级
2019-04-27
取消掉Transfer-Encoding:chunked
2019-04-27
HTTP协议中的Tranfer-Encoding:chunked编码解析
2019-04-27
JavaScript面向对象编程
2019-04-27
在Javascript中使用面向对象的编程
2019-04-27
PHP Webservice的发布与调用
2019-04-27
php反射类 ReflectionClass
2019-04-27