I am trying to mock a function that has an argument that calls another method.
I recognize to patch a function without an argument you do this
def monthly_schedule(self, month):
response = requests.get(f'http://company.com/{self.last}/{month}')
if response.ok:
return response.text
else:
return 'Bad Response!'
def test_monthly_schedule(self):
with patch('employee.requests.get') as mocked_get:
mocked_get.return_value.ok = True
mocked_get.return_value.text = 'Success'
schedule = self.emp_1.monthly_schedule('May')
mocked_get.assert_called_with('http://company.com/Schafer/May')
self.assertEqual(schedule, 'Success')
How would one mock a function that has the sort of syntax shown below?
Stock(ticker) is imported and is different than the 'Stocks' class.
from iexfinance.stocks import Stock
class Stocks:
def price(self, ticker):
price = Stock(ticker).get_price()
self.myStockData.at["price", ticker] = price
A test with this sort of nature seems to throw a 'ModuleNotFoundError' on every variation of
with patch('stocks.Stock(ticker).get_price') as mock:
with patch('Stock(ticker).get_price') as mock:
with patch('stocks.get_price') as mock:
import unittest
from unittest.mock import patch
from stocks import Stocks
class MyTestCase(unittest.TestCase):
def test_price(self):
with patch('stocks.Stock(ticker).get_price') as mock:
mock = 300.00
self.test.price('AAPL')
self.assertEqual(self.test.myStockData.at["price", 'AAPL'], 300)
For brevity, not all code is shown. Any help would be appreciated. Thanks!
Aucun commentaire:
Enregistrer un commentaire