GithubHelp home page GithubHelp logo

Comments (5)

macisamuele avatar macisamuele commented on May 21, 2024 2

@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

from testslide.

shish avatar shish commented on May 21, 2024

For context - this was causing issues with a @property that throws an exception in the unit-test environment, so somebody wants to mock it out. The current theory is that testslide is calling the property in order to find the original value so that it can un-patch it later, however everything explodes if the property raises an exception and there's no obvious workaround.

from testslide.

fornellas avatar fornellas commented on May 21, 2024

The current theory is that testslide is calling the property in order to find the original value so that it can un-patch it later, however everything explodes if the property raises an exception and there's no obvious workaround.

You'd be correct: this is how patching works. If the property is broken, then... things should break anyway :-/ There's no workaround: the property should be fixed.

self.mock_callable(host, "prop").to_return_value("patched")

This on the other hand... I'd expect to fail: a property is not... callable. It should have failed just as if one was trying to do mock_callable against a string attribute of an object. This is a bug (though not the one reported here):

import testslide

class SampleClass:
    def __init__(self):
        self.attr = "str"

class SampleTest(testslide.TestCase):
    def test_prop_callable(self) -> None:
        host = SampleClass()
        self.mock_callable(host, "attr").to_return_value("patched")
        self.assertEqual(host.attr, "patched")

yields:

blah_test.SampleTest
  test_prop_callable: ValueError: mock_callable() can only be used with callable attributes and 'str' is not.

from testslide.

printercu avatar printercu commented on May 21, 2024

If the property is broken, then... things should break anyway :-/ There's no workaround: the property should be fixed.

Property may call external service that is unavailable during tests and it will raise an error. This is what mocking should prevent.

from testslide.

fornellas avatar fornellas commented on May 21, 2024

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")

from testslide.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.