Can't mock a @property without calling it?
See original GitHub issueI’m using TestSlide
version: 2.7.0
Given:
import testslide
class SampleClass:
@property
def prop(self) -> str:
raise RuntimeError("must not be called")
def meth(self) -> str:
raise RuntimeError("must not be called")
class SampleTest(testslide.TestCase):
def test_prop_attr(self) -> None:
host = SampleClass()
self.patch_attribute(host, "prop", "patched")
self.assertEqual(host.prop, "patched")
def test_prop_callable(self) -> None:
host = SampleClass()
self.mock_callable(host, "prop").to_return_value("patched")
self.assertEqual(host.prop, "patched")
def test_meth(self) -> None:
host = SampleClass()
self.mock_callable(host, "meth").to_return_value("patched")
self.assertEqual(host.meth(), "patched")
When I run:
testslide patch_test.py
I expected this to happen:
prop()
is not called
But, instead this happened:
prop()
is called, both when using patch_attribute
and mock_callable
.
patch_test.SampleTest
test_meth
test_prop_attr: RuntimeError: must not be called
test_prop_callable: RuntimeError: must not be called
Failures:
1) patch_test.SampleTest: test_prop_attr
1) RuntimeError: must not be called
File "patch_test.py", line 15, in test_prop_attr
self.patch_attribute(host, "prop", "patched")
File "patch_test.py", line 6, in prop
raise RuntimeError("must not be called")
2) patch_test.SampleTest: test_prop_callable
1) RuntimeError: must not be called
File "patch_test.py", line 20, in test_prop_callable
self.mock_callable(host, "prop").to_return_value("patched")
File "patch_test.py", line 6, in prop
raise RuntimeError("must not be called")
Issue Analytics
- State:
- Created a year ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Moq: Setup a property without setter? - Stack Overflow
You can use .SetupGet on your mock object. eg. [Test] public void DoOneStep () { var mock = new Mock<PairOfDice>(); mock.SetupGet(x => x....
Read more >Property setups are ignored on mocks instantiated using Mock ...
I use SetupAllProperties and have a SetupGet on a property (I'll call it propertyA ). Somewhere in the code, the propertyA will be...
Read more >unittest.mock — mock object library — Python 3.11.1 ...
Attach a mock as an attribute of this one, replacing its name and parent. Calls to the attached mock will be recorded in...
Read more >ES6 Class Mocks - Jest
mock ('./sound-player') returns a useful "automatic mock" you can use to spy on calls to the class constructor and all of its methods....
Read more >Assign value to property of mock object when method is called
Hi, is there a way to assign a value to a property of a mock object when a method of the same object...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@fornellas I can try working on this within the next 2 weeks. Internally I’ve already prepared a prototype of the possible fix and I need to reserve enough bandwidth to clean-it-up and fix tests as needed
Hum… I see your point, that’s fair.
I think the way to address this is to add (yet) more complexity on top:
There are then 2 bugs to fix: