#GoMock, callback function should assign value to outside scope variable during mocking.

5 messages · Page 1 of 1 (latest)

verbal goblet
#

Hi gophers, I have a problem and I have no clue of getting around it, are there any smart advisors that could help ?

Lets say I have a service a function being called something like this:

someData := ""
s.something.foo(arg1, callback(arg2) err{
  someData = "xxx"
return true
})

now, this function returns some value, but inside of it assigns some values to data declared above it. How do I mock this action ?

I tried something like this but it did not work out

MockSomeData := ""
mycallback := func(arg1, innercallback func(arg2) error){
  MockSomeData = "xxx"
}
s.MockSomething.EXPECT().foo(arg1, gomock.Any()).Do(mycallback)

Do you have any tips on how to go around it ? I cant really specify .RETURN(&MockSomeData) as the function does not really return the desired value but only assigns value to outer scope variable.

I tried this as suggested but does not work.

MockSomeData := ""
mycallback := func(arg1, innercallback func(arg2) error){
  *(&MockSomeData) = "xxx"
}

s.MockSomething.EXPECT().foo(arg1, gomock.Any()).Do(mycallback)

When I debug the code and the inner callback function runs the MockSomeData gets overwritten but as soon as it steps outside the scope MockSomeData is nil again.

glossy gale
#

Don't use :=, use =

verbal goblet
#

sorry, typo on my part

glossy gale
#

Npnp