Ruby Metaprogramming

Posted by Veerendra Manikonda on Stack Overflow See other posts from Stack Overflow or by Veerendra Manikonda
Published on 2013-10-03T05:50:51Z Indexed on 2013/11/03 15:54 UTC
Read the original article Hit count: 169

Filed under:
|

I am having a method which returns the price of a given symbol and i am writing a test for that method.

This is my test

 def setup
   @asset = NetAssetValue.new
 end

 def test_retrieve_price_for_symbol_YHOO
   assert_equal(33.987, @asset.retrieve_price_for_a_symbol('YHOO'))
 end

 def test_retrive_price_for_YHOO        
   def self.retrieve_price_for_a_symbol(symbol)
     33.77
   end

   assert_equal(33.97, @asset.retrieve_price_for_a_symbol('YHOO'))
 end

This is my method.

def retrieve_price_for_a_symbol(symbol)
  symbol_price = { "YHOO" => 33.987, "UPS" => 35.345, "T" => 80.90 }
  raise Exception if(symbol_price[symbol].nil?)
  symbol_price[symbol]
end

I am trying to mock the retrieve_price_for_a_symbol method by writing same method in test class but when i call it, the call is happening to method in main class not in the test class.

How do I add that method to meta class from test and how do i call it? Please help.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about metaprogramming