0

I have the following table-driven test in go:

    func Test_funcTest(t *testing.T) {
        
        mockStr := "str"
    
        type args struct {
            arg1 String
            arg2 String
        }
        
        tests := []struct {
            name    string
            args    args
            want    string
            wantErr bool
        }{
            {
                name: "test1",
                args: args{
                    arg1: "arg1",
                    arg2: "arg2",
                },
                want:    "res1",
                wantErr: false,
            },
            {
                name: "test2",
                args: args{
                    arg1: "arg1",
                    arg2: "arg2",
                },
                want: "res2",
                wantErr: false,
            },
        }
        for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {

            got, err := funcTest(tt.args.arg1, tt.args.arg2)
            if (err != nil) != tt.wantErr {
                t.Errorf("funcTest() error = %v, wantErr %v", err, tt.wantErr)
                return
            }
            if got != tt.want {
                t.Errorf("funcTest() = %v, want %v", got, tt.want)
            }
        })
    }
}

I am mocking the mockStr string that the funcTest() method uses during it's execution. test1 modifies the mockStr string and this modified version is used by test2. How can I mock two different versions of the mockStr string for these 2 tests?

Thanks!

3
  • You wrote funcTest(tt.args.client, tt.args.event), but tt.args does not have client nor event, it has arg1 and arg2. Did you mean to use those? Also, it's unclear how you want to use mockStr. Commented Feb 28, 2024 at 21:21
  • @Spiros Thanks for pointing that out, updated the code. I just want to use 2 different values of mockStr for the two tests. So basically mockStr := "str" for test1 and mockStr := "str2" for test2 Commented Feb 28, 2024 at 22:45
  • Then mockStr needs to be part of the test scenario. It's still unclear where you want to use this string anyway. Commented Feb 29, 2024 at 9:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.