S
S
SSSSTTTTAAAASSSS2021-08-30 21:17:22
go
SSSSTTTTAAAASSSS, 2021-08-30 21:17:22

Test in golang: simple function?

Good evening.
There is a simple addition function. The code is below
https://play.golang.org/p/O-ZItWH_uUS
I'm trying to write a test for it, but I can't figure out how to correctly bind the interface that is passed as an argument to the function that needs to be tested.
So far, this is what happened, and it does not work
https://play.golang.org/p/WK_XU7igvV2

I would be grateful if someone tells me how to connect it correctly so that it works.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-08-30
@SSSSTTTTAAAASSSS

Alternatively, you can do something like this

package main

import "testing"

func Test_sum(t *testing.T) {

    tt := []struct {
        args GetArgs
        sum int
    }{
        {Args {2, 4}, 6},
        {Args {2, -2}, 0},
    }

    for _, tc := range tt {
        tci := tc.args.(GetArgs)
        s := Sum(tci)
        if s != tc.sum {
            t.Errorf("sum of %v and %v should be %v, received- %v", tc.args.GetA(), tc.args.GetB(), tc.sum, s)
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question