How to mock a property #525
-
Hi! I'm a bit unsure how to go about mocking "new" syntax properties with the handy implicit get. For example: class_name Foo
var _bar: Bar
var prop: int:
get: return _bar.something.else How would I go about mocking the "call" to the implicit getter function assuming I mock I've tried a couple things, such as: var mock_foo : Foo = mock(Foo)
do_return(42).on(mock_foo).prop # meaningless, I think, seems to do nothing
do_return(42).on(mock_foo).get("prop") # does nothing? It almost seems like this new syntax does not get mocked at all, as the underlying class's code is called... The only path where I had success was to stop using the nice new syntax, like this: # [...]
var prop: int: get = _get_prop
func _get_prop() -> int:
return _bar.something.else
# [...]
do_return(42).on(mock_foo)._get_prop() # works!
do_return(42).on(mock_foo).get("prop") # also works! mysteriously... I think the issue is that the new syntax is doing some magic that I don't understand. Or perhaps it isn't implemented yet? Thanks for any help/advice you have! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi, The get/set functional syntax is actual not supported to mock. For your example
There are two options to mock it.
I would prefer option a) |
Beta Was this translation helpful? Give feedback.
Hi, The get/set functional syntax is actual not supported to mock.
For your example
There are two options to mock it.
a) mock class
Bar
and set the _bar value with the mocked instance in your class Foob) implement a custom getter function and mock this function
like you did in your example
I would prefer option a)