Mock child workflows

Hi,
I have a use case where from my workflow (MyWorkFlow), I execute a child workflow in another service.
Now, I am writing a unit test for MyWorkFlow and have to mock this child workflow. I am not sure how to register this workflow in the test env. This is what I tried-

  1. Register workflow with test workflow environment
    s.env.RegisterWorkflow(“ChildWFInAnotherService”)

  2. Mock the response
    s.env.OnWorkflow(“ChildWFInAnotherService”, mock.Anything).Return(mockResponse).
    This throws an error on Register -
    test panicked: expected a func as input but was string
    I think I need to give the child workflow name as a function reference, but not sure how if its on another service. Any suggestions?

Thanks

Hello @meg

I think this example is doing something similar,

Let me know if it helps,

Antonio

Thanks for you reply @antonio.perez .
If you see my example, this is what I tried. But since the child workflow belongs to a different service, and hence not in this repository, it doesn’t recognize it.
In the sample code, the child workflow code is available.

Hi @meg

sorry,

I guess you have to provide an implementation to the RegisterWorkflow method. It can be an empty one with the same signature.

Let me ask the team and see if there are other options.

Antonio

Hi @antonio.perez ,

Not sure I understand. Can you please elaborate?

Thanks!

@meg

in your test, you can declare the following function

func SampleChildWorkflow(ctx workflow.Context, name string) (string, error) {
return "greeting", nil
}

and register it as follows

	env.RegisterWorkflow(SampleChildWorkflow)
	env.OnWorkflow("SampleChildWorkflow", mock.Anything, mock.Anything).Return(
		func(ctx workflow.Context, name string) (string, error) {
			return "Hi " + name + "!", nil
		},
	)

Antonio

1 Like