Please come up with some scenario where mocking is required, write the Python class, and the Python unit test that included the patch that enables the mocking. You may patch a single value using return_value, or multiple values with side_effect.   from territory import Territory from army import Army from territory_test import TestDoBattle t = Territory(10, Army()) t.do_battle(Army()) print(t.curr_territory) unittest.main() ----- class Army: def get_curr_strength(self): return 0 # pretend there is code here that returns an actual value ------ from army import Army class Territory: def __init__(self, initial_territory, this_army): self.curr_territory = initial_territory self.this_army = this_army def do_battle(self, enemy_army): self.curr_territory += self.this_army.get_curr_strength() - \ enemy_army.get_curr_strength() ------     something similar to this code: from territory import Territory from army import Army import unittest from unittest.mock import patch class TestThisArmyTenEnemyFiveIncreasesTerritoryByFive(unittest.TestCase): @patch('army.Army.get_curr_strength', side_effect = (10,5)) def runTest(self, _): t = Territory(10, Army()) t.do_battle(Army()) self.assertEqual(15, t.curr_territory)    unittest.main()

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 7PE
icon
Related questions
Question

Please come up with some scenario where mocking is required, write the Python class, and the Python unit test that included the patch that enables the mocking. You may patch a single value using return_value, or multiple values with side_effect.

 

from territory import Territory
from army import Army
from territory_test import TestDoBattle

t = Territory(10, Army())
t.do_battle(Army())
print(t.curr_territory)

unittest.main()

-----

class Army:
def get_curr_strength(self):
return 0 # pretend there is code here that returns an actual value

------

from army import Army
class Territory:
def __init__(self, initial_territory, this_army):
self.curr_territory = initial_territory
self.this_army = this_army

def do_battle(self, enemy_army):
self.curr_territory += self.this_army.get_curr_strength() - \
enemy_army.get_curr_strength()
------

 

 

something similar to this code:

from territory import Territory
from army import Army
import unittest
from unittest.mock import patch

class TestThisArmyTenEnemyFiveIncreasesTerritoryByFive(unittest.TestCase):
@patch('army.Army.get_curr_strength', side_effect = (10,5))
def runTest(self, _):
t = Territory(10, Army())
t.do_battle(Army())
self.assertEqual(15, t.curr_territory)

  
unittest.main()
  

 

Expert Solution
steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Math class and its different methods
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
New Perspectives on HTML5, CSS3, and JavaScript
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning
Np Ms Office 365/Excel 2016 I Ntermed
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:
9781337508841
Author:
Carey
Publisher:
Cengage