首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > Ruby Rails >

MiniTest:Mock Ruby1.9 标准库支持

2012-09-03 
MiniTest::Mock Ruby1.9 标准库支持官方说明如下主要一个expect一个verify#Expect that method name is ca

MiniTest::Mock Ruby1.9 标准库支持
官方说明如下
主要一个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) # => trueargs 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


给个标准库地址:http://www.ruby-doc.org/stdlib-1.9.3/


之前的用法:

class AdminMailer    def mail_admins( message )        # ... send emails to all admins    endendclass ErrorHandler    def initialize( mailer )        @mailer = mailer    end    def execute        begin            yield        rescue RuntimeError => err            @mailer.mail_admins( err.message )        end    endendrequire '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    endend


assert( boolean, [message] )#True if booleanassert_equal( expected, actual, [message] )assert_not_equal( expected, actual, [message] )#True if expected == actualassert_match( pattern, string, [message] )assert_no_match( pattern, string, [message] )#True if string =~ patternassert_nil( object, [message] )assert_not_nil( object, [message] )#True if object == nilassert_in_delta( expected_float, actual_float, delta, [message] )#True if (actual_float - expected_float).abs <= deltaassert_instance_of( class, object, [message] )#True if object.class == classassert_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

热点排行