title: How to return a dynamic value from a Mocha mock in Ruby
body: <p>The gist of my problem is as follows:-</p>
  
  <p>I'm writing a Mocha mock in Ruby for the method represented as "post_to_embassy" below. It is not really our concern, for the purpose of describing the problem, what the actual method does. But I need the mock to return a dynamic value. The proc '&amp;prc' below is executing rightly in place of the actual method. But the "with" method in Mocha only allows for boolean values to be returned. So the code below outputs nil. I need it to output the value being passed through orderInfoXml. Does anyone know of an alternate method I can use?</p>
  
  <pre><code>require 'rubygems'
  require 'mocha'
  include Mocha::API
  
  class EmbassyInterface 
    def post_to_embassy(xml)
      puts "This is from the original class:-"
      puts xml
      return xml
    end
  end
  
  orderInfoXml = "I am THE XML"
  
  mock = EmbassyInterface.new
  prc = Proc.new do |orderXml| 
    puts "This is from the mocked proc:-"
    puts orderXml
    orderXml
  end
  
  mock.stubs(:post_to_embassy).with(&amp;prc)
  mock_result = mock.post_to_embassy(orderInfoXml)
  p mock_result
  #p prc.call("asd")
  </code></pre>
  
  <p>output:-</p>
  
  <pre><code>This is from the mocked proc:-
  I am THE XML
  nil
  </code></pre>