Thank you Gregor for your quick reply!

Anything special I need to think about this go routine logic in my unit 
test. It might be still running after test returns..?

On Thursday, September 10, 2020 at 5:26:13 PM UTC+10 be...@pferdewetten.de 
wrote:

> If I were you, I'd pass in an interface value to functionA with the 
> signature
>
> type Ber interface{
> functionB(io.Reader, io.Writer)
> }
>
> (assuming that zipReader is an io.Reader, zipWriter is an io.Writer, you 
> may have to adjust this a bit).
>
> Then make your mock functionB a method on a type that implements the Ber 
> interface, the same for the real functionB. In unit tests, pass in an 
> instance of the type with the mock method, in real life, pass in an 
> instance of the type with the real method.
>
> Note that the types could be as simple as
>
> type mockB struct{}
> func (m mockB) functionB(...)
>
> On 10.09.20 09:15, Yvonne Zhang wrote:
> > Hi,
> > I have a function streaming a zipreader to browser. It is like this.
> > func functionA(....)(body io.ReadCloser, err error){
> > 
> >    // some logic to get a zipreader
> > 
> >    body, pipeWriter := io.Pipe()
> >    zipWriter := zip.NewWriter(pipeWriter)
> > 
> >    go func(){
> >       // err := functionB(zipReader, zipWriter)call another function to 
> > prepare files and write to zipwriter
> >    If err != nil{
> >        zipWriter.Close()
> >         _ = pipeWriter.CloseWithError(err)
> >         return
> >    }
> >       zipWriter.Close()
> >        pipeWriter.Close()
> >    }()
> >    return
> > }
> > 
> > My question is about unit test about this functionA. I have mock for 
> > functionB. How do I write unit test for functionA with the mock 
> > functionB. It seems my unit test does not work well when it goes into 
> > the goroutine.
> > Many thanks!
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> > Groups "golang-nuts" group.
> > To unsubscribe from this group and stop receiving emails from it, send 
> > an email to golang-nuts...@googlegroups.com 
> > <mailto:golang-nuts...@googlegroups.com>.
> > To view this discussion on the web visit 
> > 
> https://groups.google.com/d/msgid/golang-nuts/4ca0817a-f1f7-44a9-be3d-3584bcb61b8an%40googlegroups.com
>  
> > <
> https://groups.google.com/d/msgid/golang-nuts/4ca0817a-f1f7-44a9-be3d-3584bcb61b8an%40googlegroups.com?utm_medium=email&utm_source=footer
> >.
>
> -- 
> Gregor Best
> be...@pferdewetten.de
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/33648e9a-4011-4d02-a07b-01b506cf5cd1n%40googlegroups.com.

Reply via email to