Closed emman27 closed 5 years ago
(Fixing in test code can be done via explicit conversion, but it looks awful)
func Test_i(t *testing.T) {
i := NewMockiface()
When(i.f()).Then(func([] Param) ReturnValues {
c := make(chan s)
e := make(chan error)
go func() {
c <- s{}
close(c)
close(e)
}()
return []ReturnValue{(<-chan s)(s), (<-chan error)(e)}
})
}
Hi @emman27 and thanks for reporting this. I will have a look at this as soon as possible (currently travelling with no proper access to a computer). Will get back to you.
Hi @emman27, had a closer look at this now. Unfortunately, I don't think there is a way to clean this up. Your explicit conversions are the way to go. I know this is not very satisfying, but in general using the callback functions in pegomock is not very satisfying, because in pegomock's implementation I have to use a lot of reflection to deal with the interafce{}
s. The only solution would be generics, which we don't have (yet).
@emman27 thinking about it a little more, let me check if we can have a special case for this in the implementation. Maybe something along the lines of when the target type is a channel with direction, a channel with no direction as source type is also fine. Will get back to you.
@emman27 Could you please try the develop
branch and see if that fixes your problem? If so, I'll merge it into mainline and make a new release. Thank you.
Thanks @petergtz
Just wondering what I need to do to make this work - from what I understand - the changes are on the mock generation side? So I should reinstall the CLI tool and regenerate my mocks, followed by re-running my test cases? Doesn't seem to be working for me, unfortunately.
(Here's my step-through process, in case I've missed something)
go get github.com/petergtz/pegomock
cd ~/go/src/github.com/petergtz/pegomock
git checkout develop
git fetch && git pull
go install github.com/petergtz/pegomock
cd ~/myproj (I'm using go modules)
pegomock generate --package myproj --use-experimental-model-gen -o interface.generated_test.go ./interface.go
go test ./ (I have some test that uses []ReturnValue{})
@emman27 The line
go install github.com/petergtz/pegomock
should be
go install github.com/petergtz/pegomock/pegomock
because the binary sits in that sub-directory.
With that change I think it should work for your case. However, I just noticed that another case is not working anymore now. Will provide a fix soon.
@emman27 Just provided a fix that should make channels work in all cases. Please try with latest from develop
. Make sure to install the pegomock binary as described in https://github.com/petergtz/pegomock/issues/84#issuecomment-491740230.
@petergtz works great for me! Thanks!
Code example (modified from my own code):
Generates the following error
Seems it can't deal with the direction of the channels. Any possibility that this can be cleaned up?