Reputation: 127
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.
Upvotes: 1
Views: 172
Reputation: 4489
Assuming you don't really want to mock the method you're testing...
You are currently defining your mock on the instance of the test class. You can add your mock directly to the @asset
object:
def test_retrive_price_for_YHOO
def @asset.retrieve_price_for_a_symbol(symbol)
33.77
end
assert_equal(33.97, @asset.retrieve_price_for_a_symbol('YHOO'))
end
Upvotes: 2
Reputation: 621
Instead of re-defining the method inside, you need to mock it out.
Replace the method definition inside the test with
@asset.expects(:retrieve_price_for_a_symbol).with('YHOO').returns(33.97)
Upvotes: 2