Gut icon indicating copy to clipboard operation
Gut copied to clipboard

How to double a class with _init(value).

Open lars-hoeck opened this issue 2 years ago • 3 comments

Hey, on the Doubles wiki page you mentioned, that it is possible to double a class which has for example _init(value). I couldnt get it working. It would be nice to have an example in the wiki.

lars-hoeck avatar Feb 12 '23 14:02 lars-hoeck

Good find. Here is the specific section where the link should have gone: https://github.com/bitwes/Gut/wiki/Stubbing#stubbing-method-paramter-defaults. But that section doesn't explain in detail how to stub _init. The following should be added to the Doubling or Stubbing page.

To stub _init you would do the following: Given:

class StubMyInit:
    var bar = 1
    func _init(foo):
        bar = foo

Stubbing _init in a test like this:

func test_foo_init_param_stub():
    # stubbing _init parameters must be done using the class, and 
    # happen before calling double since the defaults will be 
    # used when new is called.
    stub(StubMyInit, '_init').param_defaults([99])
    var dbl = double(StubMyInit).new()
    assert_eq(dbl.bar, 99)

bitwes avatar Feb 12 '23 18:02 bitwes

Test.zip This does not work for me. I use Godot 3.5 and uploaded a test project. The file for testing are in the folder test.

lars-hoeck avatar Feb 13 '23 18:02 lars-hoeck

Sorry, I forgot another condition is that all _init params must have defaults. Changing _init to this made the test pass:

func _init(foo=1):

bitwes avatar Feb 19 '23 00:02 bitwes