Add tests for PasteService

master
Nick Krichevsky 2019-03-09 23:31:26 -05:00
parent 5c7d1c7da7
commit 9e329458d6
1 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,58 @@
package handler
import (
"testing"
"github.com/google/uuid"
"github.com/ollien/updown/repository"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type MockPasteRepository struct {
mock.Mock
}
func (repo *MockPasteRepository) GetPaste(handle uuid.UUID) (repository.Paste, error) {
args := repo.Called(handle)
return args.Get(0).(repository.Paste), args.Error(1)
}
func (repo *MockPasteRepository) PutPaste(title string) (repository.Paste, error) {
args := repo.Called(title)
return args.Get(0).(repository.Paste), args.Error(1)
}
func (repo *MockPasteRepository) UpdatePaste(paste repository.Paste) error {
args := repo.Called(paste)
return args.Error(0)
}
func setupPasteService() *PasteService {
mockRepo := &MockPasteRepository{}
return &PasteService{
db: mockRepo,
pasteDir: "/tmp/pastes/",
}
}
func TestCreatePaste(t *testing.T) {
service := setupPasteService()
mockRepo := service.db.(*MockPasteRepository)
testPaste := repository.Paste{
ID: 23,
Title: "My Awesome Paste",
Handle: uuid.New(),
}
mockRepo.On("PutPaste", testPaste.Title).Return(testPaste, nil).Once()
paster, err := service.CreatePaste(testPaste.Title)
mockRepo.AssertExpectations(t)
assert.Nil(t, err)
assert.Equal(t, testPaste, paster.Paste)
assert.Equal(t, service.pasteDir, paster.pasteDir)
}