mock vs monkey patching

Viewed 442

I'm still not 100% clear mocking/stubing vs simply monkey patching? For python, why dont we always just monkey patch?
Example below
a) mockBinanceConn is a magic mock - return values stubbed (We could just monkey patch instead of using a mock)
b) analyticService.triggerCalculator below was monkey patched, very conveniently using a simple lambda

Question is, why dont we always monkey patch? For below example, I can see one advantage with mock - you don't need construct the "mockBinanceConn".

@patch('Market.Crypto.binance.BinanceConnector')
def testAnalyticService_RunAlgo_BACKTEST(self, mockBinanceConn):
    mockBinanceConn.getTrades.return_value = self.dummyTrades
    mockBinanceConn.getOrderBook.return_value = self.dummyOrderBook
    self.mktService.marketConn[mktAccess.MarketAccessService.PROVIDER_BINANCE] = mockBinanceConn

    strategies = self.dao.getStrategies()
    self.mktService.reloadMarketData(strategies)

    # monkey patch analyticService to speed up test (We're not testing Algo here)
    dummyTriggerCalculator = lambda mode, cob, strategyId, instrumentId, testId : 1
    self.analyticService.triggerCalculator = dummyTriggerCalculator

    self.analyticService=.calcPnl()
0 Answers
Related