question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't mock a @property without calling it?

See original GitHub issue

I’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:open
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
macisamuelecommented, Aug 31, 2022

@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

0reactions
fornellascommented, Aug 18, 2022

Hum… I see your point, that’s fair.

I think the way to address this is to add (yet) more complexity on top:

  • When patching, check whether the attribute is a property.
  • If it is, then save the property itself (to unpatch), and not the property value.

There are then 2 bugs to fix:

  • Make the test below pass.
  • Have mock_callable against a property / attribute fail.
import testslide

class SampleClass:
    @property
    def prop(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")
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found