FAQ
I've asked about mock testing in Go a few times before and I have not been
able to find a great solution. I'm currently using qur/withmock
<https://godoc.org/github.com/qur/withmock> which works decently except
that it is a little buggy and takes a very long time to run (it copies and
rewrites mocked packages in a separate directory).

Here is an example of something that I am currently using qur/withmock for:
https://github.com/hack4impact/transcribe4all/blob/ibm-websockets/transcription/ibm_test.go

The code I am testing uses websockets to consume the IBM speech to text
API. It's especially important that I mock the API calls because a real
transcription can take an hour. Does anyone have suggestions for a better
way to test this code?

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Tamás Gulácsi at Apr 18, 2016 at 4:30 am
    For that little code, create interfaces with the needed functions (only a handful), and create the real and a dummy/mock/test implementation.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ben Sandler at Apr 18, 2016 at 5:13 am
    Can you give an example?
    On Monday, April 18, 2016 at 12:29:59 AM UTC-4, Tamás Gulácsi wrote:

    For that little code, create interfaces with the needed functions (only a
    handful), and create the real and a dummy/mock/test implementation.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Tamás Gulácsi at Apr 18, 2016 at 9:22 am
    2016. április 18., hétfő 7:13:42 UTC+2 időpontban Ben Sandler a következőt
    írta:
    Can you give an example?

    Mocking sql.DB: https://godoc.org/github.com/tgulacsi/go/dber#Mock
    On Monday, April 18, 2016 at 12:29:59 AM UTC-4, Tamás Gulácsi wrote:

    For that little code, create interfaces with the needed functions (only a
    handful), and create the real and a dummy/mock/test implementation.
    You have
    https://github.com/hack4impact/transcribe4all/blob/ibm-websockets/transcription/ibm_test.go#L31

    type wsdialer interface {
       Dial(a, b) wser // just as websocket.DefaultDialer.Dial
    }
    type wser interface { // just as what websocket.DefaultDialer.Dial returned
       WriteJSON...
       WriteMessage...
       ReadJSON...
    }














    and have a real implementation (the current one, maybe with some shims for
    the returning-interface type error), and a test implementation.
    The test implementation can be as simple as a struct with the awaited args
    and the thing to return on call, or as general as a struct with Expect...
    functions to fill it up as you do now.

    But nothing here needs a source level programmatic rewrite.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Andrey Nering at Apr 18, 2016 at 3:04 pm
    I wrote a simple DB fixture package, so I don't need mock SQL drivers.

    Em segunda-feira, 18 de abril de 2016 06:22:05 UTC-3, Tamás Gulácsi
    escreveu:
    2016. április 18., hétfő 7:13:42 UTC+2 időpontban Ben Sandler a következőt
    írta:
    Can you give an example?

    Mocking sql.DB: https://godoc.org/github.com/tgulacsi/go/dber#Mock
    On Monday, April 18, 2016 at 12:29:59 AM UTC-4, Tamás Gulácsi wrote:

    For that little code, create interfaces with the needed functions (only
    a handful), and create the real and a dummy/mock/test implementation.
    You have
    https://github.com/hack4impact/transcribe4all/blob/ibm-websockets/transcription/ibm_test.go#L31

    type wsdialer interface {
    Dial(a, b) wser // just as websocket.DefaultDialer.Dial
    }
    type wser interface { // just as what websocket.DefaultDialer.Dial
    returned
    WriteJSON...
    WriteMessage...
    ReadJSON...
    }














    and have a real implementation (the current one, maybe with some shims for
    the returning-interface type error), and a test implementation.
    The test implementation can be as simple as a struct with the awaited args
    and the thing to return on call, or as general as a struct with Expect...
    functions to fill it up as you do now.

    But nothing here needs a source level programmatic rewrite.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Scott Kidder at Apr 19, 2016 at 8:34 pm
    I've had great results with the Mockery
    package: https://github.com/vektra/mockery

    It'll generate mocks for all interfaces found within a given package. You
    can then instantiate the mocks in your tests, set expectations for calls to
    the mocks, and make assertions on those calls after the function-under-test
    returns. I run the mockery tool only when I need to generate new mocks or
    update a mock to reflect a change in the interface.

    --Scott Kidder
    On Sunday, April 17, 2016 at 2:40:02 PM UTC-7, Ben Sandler wrote:

    I've asked about mock testing in Go a few times before and I have not been
    able to find a great solution. I'm currently using qur/withmock
    <https://godoc.org/github.com/qur/withmock> which works decently except
    that it is a little buggy and takes a very long time to run (it copies and
    rewrites mocked packages in a separate directory).

    Here is an example of something that I am currently using qur/withmock
    for:
    https://github.com/hack4impact/transcribe4all/blob/ibm-websockets/transcription/ibm_test.go

    The code I am testing uses websockets to consume the IBM speech to text
    API. It's especially important that I mock the API calls because a real
    transcription can take an hour. Does anyone have suggestions for a better
    way to test this code?
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 17, '16 at 9:39p
activeApr 19, '16 at 8:34p
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase