Suppose we have a class UserServicewith an attribute current_user. Suppose it is used in a class AppService.
UserService
current_user
AppService
We have AppServicecovered in tests. In the test setup, we cross out current_userwith some mock value:
UserService.current_user = 'TestUser'
Suppose we decide to rename current_userto active_user. We will rename it to UserService, but forget to make changes to its use at AppService.
active_user
We run the tests and they pass! In the test setup, an attribute is added current_user, which is still (erroneously, but successfully) used in AppService.
Now our tests are useless. They pass, but the application does not work.
We cannot rely on our test suite ==> TDD is not possible.
Does TDD crash in Python?
OK, I found a solution. The Python library Mock does what I want .
Mock
Below is the code I'm finishing in.
Definition of model and service:
class User(object): def __init__(self): self.roles = [] class UserService(object): def get_current_user(self): return None # get from environment, database, etc. current_user = property(get_current_user) class AppService(object): def __init__(self, userService): self.userService = userService def can_write(self): return 'admin' in self.userService.current_user.roles
Here's how to test a method can_write AppServicewith different users:
can_write
class AppServiceTests(unittest.TestCase): def test_can_write(self): user = User() @patch_object(UserService, 'current_user', user) def can_write(): appService = AppService(UserService()) return appService.can_write() user.roles = ['admin'] self.assertTrue(can_write()) user.roles = ['user'] self.assertFalse(can_write())
If you rename a property current_useronly in a class UserService, you will get an error when trying to fix an object. This is the behavior I was looking for.
TDD, Python. , TDD , , , . , . multiplyBy2(), 1,2,3 2,4,8, , multiplyBy2 . , 100% - , . , TDD , , , - , , . , , , , . - , . , , , . , , , , , , , .
- , current_user. - (). ; :
UserService.current_user = 'X' assertFalse(obj.predicate()) UserService.current_user = 'Y' assertTrue(obj.predicate())
OK? , . . , current_user active_user. , . , current_user, . , , , .
Source: https://habr.com/ru/post/1743428/More articles:Auto rotate support for view added via presentModalViewController? - objective-cHow to set SelectedValue attribute for Telerik RadComboBox inside RadGrid FormTemplate - ajaxHow do you create a table cell scroll defined by CSS? - htmlCalling a .net web service from a simple HTML site using jQuery - jqueryPython profiling on Windows, how do you ignore built-in functions - optimizationBest way to parse XML streams like XMPP? - pythonInfoPath List with SharePoint - infopathJava: conditional initialization? - javahttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1743432/silverlight-project-fails-to-compile&usg=ALkJrhi4oOyWD3KyldLlWhNyaYN8XTb4yQOutOfMemory newbie problem - scalaAll Articles