Moking property with execption
See original GitHub issueI 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:
- Created 5 years ago
- Comments:6 (4 by maintainers)
Top 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 >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
I will very likely support
in the next version.
I agree that having cleaner support would be nicer, although if this would work
in Python unit test framework, it would be pretty clean.
But lambda can raise an exception with a trick:
Is based on: https://stackoverflow.com/a/9547687