Skip to content

Commit

Permalink
add fails testing in usecases uc/userEdit
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Jacquot committed Aug 10, 2018
1 parent c190a46 commit 9e19957
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
2 changes: 2 additions & 0 deletions implem/mock.uc/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type MockedInteractor struct {
CommentRW *MockCommentRW
}

type TestFunc = func(interactor *MockedInteractor)

type SimpleLogger struct{}

func (SimpleLogger) Log(logs ...interface{}) {
Expand Down
6 changes: 3 additions & 3 deletions uc/userEdit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func (i interactor) UserEdit(userName string, fieldsToUpdate map[domain.UserUpda
if err != nil {
return nil, "", err
}
if user.Name != userName {
return nil, "", errWrongUser
}
if user == nil {
return nil, "", ErrNotFound
}
if user.Name != userName {
return nil, "", errWrongUser
}

domain.UpdateUser(user,
domain.SetUserName(fieldsToUpdate[domain.UserName]),
Expand Down
66 changes: 65 additions & 1 deletion uc/userEdit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package uc_test
import (
"testing"

"errors"

"github.com/err0r500/go-realworld-clean/domain"
mock "github.com/err0r500/go-realworld-clean/implem/mock.uc"
"github.com/err0r500/go-realworld-clean/testData"
Expand All @@ -18,8 +20,15 @@ func TestEditUser_happyCase(t *testing.T) {
expectedUser.Email = testData.User("jane").Email
rick := testData.User("rick")
jane := testData.User("jane")

urw := func(i *mock.MockedInteractor) {
i.UserRW.EXPECT().GetByName(rick.Name).Return(&rick, nil).Times(1)
}

i := mock.NewMockedInteractor(mockCtrl)
i.UserRW.EXPECT().GetByName(rick.Name).Return(&rick, nil).Times(1)
i.UserRW.EXPECT().GetByName(rick.Password).Return(&rick, nil).AnyTimes()
urw(&i)

i.UserValidator.EXPECT().CheckUser(expectedUser).Return(nil).Times(1)
i.UserRW.EXPECT().Save(expectedUser).Return(nil).Times(1)
i.AuthHandler.EXPECT().GenUserToken(expectedUser.Name).Return("token", nil).Times(1)
Expand All @@ -34,3 +43,58 @@ func TestEditUser_happyCase(t *testing.T) {
assert.Equal(t, expectedUser, *retUser)

}

func TestInteractor_UserEdit_fails(t *testing.T) {
rick := testData.User("rick")

invalidCalls := map[string]mock.TestFunc{
"error return on uRW.GetByName": func(i *mock.MockedInteractor) {
i.UserRW.EXPECT().GetByName(gomock.Any()).Return(nil, errors.New(""))
},
"nil, nil return on uRW.GetByName": func(i *mock.MockedInteractor) {
i.UserRW.EXPECT().GetByName(gomock.Any()).Return(nil, nil)
},
"uRW.GetByName returns wrong name": func(i *mock.MockedInteractor) {
i.UserRW.EXPECT().GetByName(gomock.Any()).Return(&domain.User{Name: "hi there"}, nil)
},
"user not validated": func(i *mock.MockedInteractor) {
i.UserValidator.EXPECT().CheckUser(gomock.Any()).Return(errors.New(""))
},
"failed to save the user": func(i *mock.MockedInteractor) {
i.UserRW.EXPECT().Save(gomock.Any()).Return(errors.New(""))
},
"failed to gen token": func(i *mock.MockedInteractor) {
i.AuthHandler.EXPECT().GenUserToken(gomock.Any()).Return("", errors.New("")).AnyTimes()
},
}

// same as the happy case but with any parameter and called any number of times (including 0)
validCalls := func(i *mock.MockedInteractor) {
i.UserRW.EXPECT().GetByName(gomock.Any()).Return(&rick, nil).AnyTimes()
i.UserValidator.EXPECT().CheckUser(gomock.Any()).Return(nil).AnyTimes()
i.UserRW.EXPECT().Save(gomock.Any()).Return(nil).AnyTimes()
i.AuthHandler.EXPECT().GenUserToken(gomock.Any()).Return("token", nil).AnyTimes()
}

{ // just to be sure, validCalls don't send any error
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
i := mock.NewMockedInteractor(mockCtrl)
validCalls(&i)
_, _, err := i.GetUCHandler().UserEdit("rick", nil)
assert.NoError(t, err)
}

for testName, invalidCall := range invalidCalls {
t.Run(testName, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
i := mock.NewMockedInteractor(mockCtrl)
invalidCall(&i) // put the tested call first (important)
validCalls(&i) // then fill the gaps with valid calls

_, _, err := i.GetUCHandler().UserEdit("rick", nil)
assert.Error(t, err)
})
}
}

0 comments on commit 9e19957

Please sign in to comment.