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.

Moking property with execption

See original GitHub issue

I would like to mock and object attribute and when the attribute is called I would like to raise an exception. I would like to do something like this:

def test(self):
    element = mock()
    when(element).text.thenRaise(ValueError('foo'))

But because when can only mock method, the above does not quite work. I also tried several other ways, but could not get it working. Is there a way to make the mock attribute to raise and exception?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kastecommented, Dec 23, 2018

I will very likely support

prop = mock()
when(prop).__get__(...).thenRaise(ValueError)
m = mock({'tx': prop})

in the next version.

0reactions
aaltatcommented, Dec 23, 2018

I agree that having cleaner support would be nicer, although if this would work

prop = mock()
when(prop).__get__(...).thenRaise(ValueError)
m = mock({'tx': prop})

in Python unit test framework, it would be pretty clean.

But lambda can raise an exception with a trick:

    def test_d(self):
        m = mock()
        m.__class__.tx = property(lambda _: exec('raise(ValueError())'))
        with self.assertRaises(ValueError):
            m.tx

Is based on: https://stackoverflow.com/a/9547687

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - How can I mock a property to raise an exception?
You can use PropertyMock for this purpose: import mock class A(object): @property def prop(self): pass a = A() type(a).prop = mock.
Read more >
Mock Properties | JustMock Documentation - Telerik
The property get can be mocked like any other method call. You can arrange a return statement for a specific call (using Returns),...
Read more >
matlab.mock.actions.ThrowException class - MathWorks
To specify that the framework throws an exception when a mock object method is invoked or when a mock object property is set...
Read more >
unittest.mock — mock object library — Python 3.11.1 ...
Useful for raising exceptions or dynamically changing return values. The function is called with the same arguments as the mock, and unless it...
Read more >
Properties - Unit Testing in C#
In case a property of the mock should behave like an automatic property, developers can instruct Moq to track the values passed to...
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