The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Mocking Ruby Classes?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jeremy Voorhis

Posts: 212
Nickname: jvoorhis
Registered: Oct, 2005

Jeremy Voorhis is a Rubyist in northeast Ohio.
Mocking Ruby Classes? Posted: Jul 10, 2006 8:58 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Jeremy Voorhis.
Original Post: Mocking Ruby Classes?
Feed Title: JVoorhis
Feed URL: http://feeds.feedburner.com/jvoorhis
Feed Description: JVoorhis is a Rubyist in northeast Ohio. He rambles about Ruby on Rails, development practices, other frameworks such as Django, and on other days he is just full of snark.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jeremy Voorhis
Latest Posts From JVoorhis

Advertisement

Maybe I missed something, but today I was looking for a generic mocking framework for Ruby that would allow me to create a mock class, not just a mock instance. I almost achieved this with Jim Weirich’s excellent FlexMock, and decided not to try out Test::Unit::Mock or SchMock just yet. Instead, I ended up writing the code at the end of this post. If you have a favorite mocking tool that I haven’t mentioned, or know how to mock classes with an existing tool, please post a comment!

Here is the code for SimpleMock:

module SimpleMock
  module Recorder
    def messages_received
      @messages_received ||= []
    end

    def method_missing message, *args, &block
      messages_received << { :message => message.to_s, :arguments => args }
    end

    def message_received? message, *args
      message = message.to_s
      !messages_received.select { |m| m[:message]  message && m[:arguments]  args }.empty?
    end
  end

  class Base
    include Recorder
    extend Recorder
  end

  module Assertions
    def assert_message_received mock, message, args
      error_message = ”#{mock}.#{message}(#{args.join(’,’)}) expected but not received.\n” +
          ”#{mock} received the following messages: \n #{mock.messages_received.inspect}” 

      assert mock.message_received?(message, args), error_message
    end
  end
end
Here is how it is used:

class Entry < SimpleMock::Base; end

class EntriesController < ActionController::Base
  include RapidResource::Crud
  # Re-raise errors caught by the controller.
  def rescue_action(e) raise e end
end

class RapidResourceTest < Test::Unit::TestCase
  include SimpleMock::Assertions

  def setup
    @controller = EntriesController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
  end

  def test_find_member_should_find_entry_by_id
    get :show, :id => 7
    assert_message_received Entry, :find, 7
  end
end

Read: Mocking Ruby Classes?

Topic: Lessons From Hpricot Previous Topic   Next Topic Topic: Ruby : nil.id => 4

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use