Merhaba arkadaşlar;
Go ile test yazarken mockladığım bir kaç bağımlılık yüzünden sorun yaşıyorum. Yardımcı olursanız sevinirim.
şöyle bir auth servisim var
type IAuthService interface {
Signup(dto *SignupDto) error
Signin(dto *SigninDto) (string, error)
}
type authServiceRepositories struct {
account account.IAccountRepository
}
type authService struct {
repositories authServiceRepositories
helpers IAuthHelper
}
sorun yasadığım kısım ise burası. AccountRepository ve AccountHelpers'ın mocku var. AccountRepository *Account
set ediyor. Compare passwordda dto dan gelen plain password ile *Account
'tan gelen hash ile karşılaştırıyor. Muhtemelen test tarafında *Account
'un işaret ettiği adres farklı diye testi geçemiyor ve ne yaptıysam olmadı
func (s *authService) Signin(dto *SigninDto) (string, error) {
acc := &models.Account{
Email: dto.Email,
}
err := s.repositories.account.GetAccountByEmail(acc)
if s.helpers.ComparePassword(acc, dto) {
return "", err
}
return "", PasswordDoesNotMatchError
}
Metodun boş string dönmesine takılmayın.
Mocklar su sekilde
// mock auth repository
type MockAccountRepository struct {
mock.Mock
DB *sqlx.DB
}
func (m *MockAccountRepository) GetAccountByEmail(a *models.Account) error {
args := m.Called(a)
return args.Error(0)
}
func (m *MockAccountRepository) CreateAccount(a *models.Account) error {
args := m.Called(a)
return args.Error(0)
}
// mock auth helpers
type MockAuthHelper struct {
mock.Mock
}
func (m *MockAuthHelper) ComparePassword(acc *models.Account, dto *auth.SigninDto) bool {
args := m.Called(acc, dto)
return args.Bool(0)
}
func (m *MockAuthHelper) HashPassword(dto *auth.SignupDto) []byte {
args := m.Called(dto)
return args.Get(0).([]byte)
}
test yapım şu şekilde
t.Run("it should signin by email and password", func() {
mockAccountRepo := new(mocks.MockAccountRepository)
mockAuthHelper := new(mocks.MockAuthHelper)
authService := auth.AuthService(mockAccountRepo, mockAuthHelper)
var err error
// var token string
adto := &auth.SigninDto{
Email: "jhon@doe.com",
Password: "1234",
}
acc := &models.Account{
Email: adto.Email,
}
mockAccountRepo.On("GetAccountByEmail", &models.Account{
Email: adto.Email,
}).
Return(nil).
Run(func(args mock.Arguments) {
// acc set etmeye calisiyorum.
arg := args.Get(0).(*models.Account)
arg.Password = []byte("$2a$10$89Uf6Q4Tm6KLaCG8LpuOne8kIsVpA7Zx2FvY9Ak8iR5ykxOwUpU0W")
})
mockAuthHelper.On("ComparePassword", acc, adto).Return(true)
// t.Nil(err)
token, err := authService.Signin(adto)
t.Nil(err)
t.Equal(token, "")
})
terminal çıktısında bu şekilde
Diff: 0: FAIL: (*models.Account=&{ jhon@doe.com [36 50 97 36 49 48 36 56 57 85 102 54 81 52 84 109 54 75 76 97 67 71 56 76 112 117 79 110 101 56 107 73 115 86 112 65 55 90 120 50 70 118 89 57 65 107 56 105 82 53 121 107 120 79 119 85 112 85 48 87]}) != (*models.Account=&{ jhon@doe.com []})
1: PASS: (*auth.SigninDto=&{jhon@doe.com 1234}) == (*auth.SigninDto=&{jhon@doe.com 1234}) [recovered]
panic:
mock: Unexpected Method Call
-----------------------------
ComparePassword(*models.Account,*auth.SigninDto)
0: &models.Account{ID:"", Email:"jhon@doe.com", Password:[]uint8{0x24, 0x32, 0x61, 0x24, 0x31, 0x30, 0x24, 0x38, 0x39, 0x55, 0x66, 0x36, 0x51, 0x34, 0x54, 0x6d, 0x36, 0x4b, 0x4c, 0x61, 0x43, 0x47, 0x38, 0x4c, 0x70, 0x75, 0x4f, 0x6e, 0x65, 0x38, 0x6b, 0x49, 0x73, 0x56, 0x70, 0x41, 0x37, 0x5a, 0x78, 0x32, 0x46, 0x76, 0x59, 0x39, 0x41, 0x6b, 0x38, 0x69, 0x52, 0x35, 0x79, 0x6b, 0x78, 0x4f, 0x77, 0x55, 0x70, 0x55, 0x30, 0x57}}
1: &auth.SigninDto{Email:"jhon@doe.com", Password:"1234"}
The closest call I have is:
ComparePassword(*models.Account,*auth.SigninDto)
0: &models.Account{ID:"", Email:"jhon@doe.com", Password:[]uint8(nil)}
1: &auth.SigninDto{Email:"jhon@doe.com", Password:"1234"}
Difference found in argument 0:
--- Expected
+++ Actual
@@ -3,3 +3,8 @@
Email: (string) (len=12) "jhon@doe.com",
- Password: ([]uint8) <nil>
+ Password: ([]uint8) (len=60) {
+ 00000000 24 32 61 24 31 30 24 38 39 55 66 36 51 34 54 6d |$2a$10$89Uf6Q4Tm|
+ 00000010 36 4b 4c 61 43 47 38 4c 70 75 4f 6e 65 38 6b 49 |6KLaCG8LpuOne8kI|
+ 00000020 73 56 70 41 37 5a 78 32 46 76 59 39 41 6b 38 69 |sVpA7Zx2FvY9Ak8i|
+ 00000030 52 35 79 6b 78 4f 77 55 70 55 30 57 |R5ykxOwUpU0W|
+ }
})
Diff: 0: FAIL: (*models.Account=&{ jhon@doe.com [36 50 97 36 49 48 36 56 57 85 102 54 81 52 84 109 54 75 76 97 67 71 56 76 112 117 79 110 101 56 107 73 115 86 112 65 55 90 120 50 70 118 89 57 65 107 56 105 82 53 121 107 120 79 119 85 112 85 48 87]}) != (*models.Account=&{ jhon@doe.com []})
1: PASS: (*auth.SigninDto=&{jhon@doe.com 1234}) == (*auth.SigninDto=&{jhon@doe.com 1234})
goroutine 9 [running]:
testing.tRunner.func1.2({0x835d80, 0xc0001e8390})
/snap/go/10389/src/testing/testing.go:1545 +0x238
testing.tRunner.func1()