Last active
August 18, 2021 07:37
-
-
Save syafdia/58ae87a8b1e5a86265215ecabdafa20d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// internal/user/delivery.go | |
package user | |
import ( | |
"context" | |
"fmt" | |
"reflect" | |
"testing" | |
"github.com/golang/mock/gomock" | |
) | |
func Test_registerUserUseCase_Execute(t *testing.T) { | |
ctrl := gomock.NewController(t) | |
defer ctrl.Finish() | |
c := NewMockUserRepo(ctrl) | |
fmt.Println(c) | |
type args struct { | |
ctx context.Context | |
input CreateUserInput | |
} | |
tests := []struct { | |
name string | |
args args | |
beforeTest func(userRepo *MockUserRepo) | |
want User | |
wantErr bool | |
}{ | |
{ | |
name: "success creating new user", | |
args: args{ | |
ctx: context.TODO(), | |
input: CreateUserInput{ | |
Email: "john.doe@example.com", | |
FirstName: "John", | |
LastName: "Doe", | |
Gender: GenderMale, | |
}, | |
}, | |
beforeTest: func(userRepo *MockUserRepo) { | |
userRepo.EXPECT(). | |
Create( | |
context.TODO(), | |
CreateUserInput{ | |
Email: "john.doe@example.com", | |
FirstName: "John", | |
LastName: "Doe", | |
Gender: GenderMale, | |
}, | |
). | |
Return( | |
User{ | |
ID: 1, | |
Email: "john.doe@example.com", | |
FirstName: "John", | |
LastName: "Doe", | |
Gender: GenderMale, | |
}, | |
nil, | |
) | |
}, | |
want: User{ | |
ID: 1, | |
Email: "john.doe@example.com", | |
FirstName: "John", | |
LastName: "Doe", | |
Gender: GenderMale, | |
}, | |
}, | |
// Next test cases. | |
// ... | |
// ... | |
} | |
for _, tt := range tests { | |
t.Run(tt.name, func(t *testing.T) { | |
mockUserRepo := NewMockUserRepo(ctrl) | |
w := ®isterUserUseCase{ | |
userRepo: mockUserRepo, | |
} | |
if tt.beforeTest != nil { | |
tt.beforeTest(mockUserRepo) | |
} | |
got, err := w.Execute(tt.args.ctx, tt.args.input) | |
if (err != nil) != tt.wantErr { | |
t.Errorf("registerUserUseCase.Execute() error = %v, wantErr %v", err, tt.wantErr) | |
return | |
} | |
if !reflect.DeepEqual(got, tt.want) { | |
t.Errorf("registerUserUseCase.Execute() = %v, want %v", got, tt.want) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment