Spying on properties
See original GitHub issueI’ve tried to use spy
on object properties, but it fails with AttributeError: can't set attribute
in unittest/mock.py
.
I’ve started looking into adding support for (through PropertyMock
), but failed to make it work without accessing/calling the property method during setup of the spy - it would be better to only call it when it’s accessed from the test.
I would appreciate any feedback / help in this regard.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Spying on properties - Jasmine Documentation
Properties are more complicated than functions. In Jasmine, you can do anything with a property spy that you can do with a function...
Read more >How to spy on a property (getter or setter) with Jasmine
spyOnProperty is implemented with a function behind the property defined through Object.defineProperty. So, you cannot use spyOnProperty to ...
Read more >jasmine Spy on Properties on Mock Objects | devnote
You can create a spy object with several properties on it quickly by passing an array or hash of properties as a third...
Read more >How to spyOn a value property (rather than a method) with ...
The right way to do this is with the spy on property, it will allow you to simulate a property on an object...
Read more >54. Spy on the getter and setter properties using ... - YouTube
In this video we will see how to spy ion the getter and setter properties using spyOnProperty method - Jasmine TestingUnit Testing with ......
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
Is there any progress?
This a nice feature that I didn’t even think about because I guess never really mocked a property before.
Below it is a draft I’ve come up in a hurry that is really buggy but get the basics done:
A test using it:
With this, the mock object returned can be used to inspect
mock_calls
to property and property value is in fact changed.Problems I’ve seen so far:
Foo.foo
without getting errors.foo
attr isn’t a mock object, so it is only possible to access mock call objects through returned object.Anyway I have to get more used to
PropertyMock
before moving on, let me know more details about your problems and implementation (maybe open a PR?) because I can try to help 😃