On Tue, Nov 19, 2019 at 6:37 AM burak serdar <bser...@computer.org> wrote:
> On Mon, Nov 18, 2019 at 10:24 PM Shane H <shane....@gmail.com> wrote: > > > > > > > > On Tuesday, November 19, 2019 at 4:11:52 PM UTC+11, burak serdar wrote: > >> > >> > >> > >> This is what I usually do in these situations: > >> > >> var amqpDial=amqp.Dial > >> func (mq *MQ) Connect() (err error) { > >> ... > >> mq.conn, err = amqpDial(mq.URI) > >> ... > >> } > >> > >> func TestConnect(t *testing.T) { > >> amqpDial=fakeDial > >> defer func() {amqpDial=amqp.Dial}() > >> ... > >> } > >> > >> > > This works, the only drawback is that I have to choose between making > amqpDial an exported (I want to say symbol here) variable, or have the test > in the same package (as opposed to a _test pkg) > > > > I've gone with the latter, put these tests into the same package, thanks. > you could have your cake *and* eat it: $> cat foo_test.go package foo func SetDialer(d Dialer) { dialer = d } $> cat all_test.go package foo_test func TestMain(m *testing.M) { foo.SetDialer(fakeDialer) os.Exit(m.Run()) } ie: only export that API for tests. hth, -s -- 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/CAAV3P_CUk4pipJ8i6vRkyH%3DLEk4MEUdEtzghMyGLGFEwd8LSgw%40mail.gmail.com.