Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/reisraff/mockings
https://github.com/reisraff/mockings
assert assertion assertions go golang library mock mocker mocking unit-test unit-testing
Last synced: 25 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/reisraff/mockings
- Owner: reisraff
- License: mit
- Created: 2019-04-25T18:43:00.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-01-27T19:09:23.000Z (almost 4 years ago)
- Last Synced: 2024-06-19T14:56:10.212Z (5 months ago)
- Topics: assert, assertion, assertions, go, golang, library, mock, mocker, mocking, unit-test, unit-testing
- Language: Go
- Size: 16.6 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Mockings
```go
package serviceimport "testing"
import "errors"
import "app/app/entity"
import "app/app/service"
import mockings "github.com/reisraff/mockings/mockings"
import assert "github.com/stretchr/testify/assert"type DatabaseWriterMock struct {
}func (self DatabaseWriterMock) Insert(entity interface{}) {
mockings.AddCall(&self, "Insert", []interface{}{entity})
}func (self DatabaseWriterMock) Save(entity interface{}) {
mockings.AddCall(&self, "Save", []interface{}{entity})
}func (self DatabaseWriterMock) Remove(entity interface{}) {
mockings.AddCall(&self, "Remove", []interface{}{entity})}
type UploadProviderMock struct {
upload.UploadProvider
}func (self UploadProviderMock) Validate(v *multipart.FileHeader) error {
result := mockings.AddCall(&self, "Validate", []interface{}{v})
return mockings.ErrorOrNil(result[0])
}func (self UploadProviderMock) GetExtension(v *multipart.FileHeader) string {
result := mockings.AddCall(&self, "GetExtension", []interface{}{v})
return result[0].(string)
}func (self UploadProviderMock) Move(v *multipart.FileHeader, v2 string) error {
result := mockings.AddCall(&self, "Move", []interface{}{v, v2})
return mockings.ErrorOrNil(result[0])
}func (self UploadProviderMock) Remove(v string) {
mockings.AddCall(&self, "Remove", []interface{}{v})
}func (self UploadProviderMock) GetUploadDir() string {
result := mockings.AddCall(&self, "GetUploadDir", []interface{}{})
return result[0].(string)
}func GetNotificationService() service.NotificationService {
notificationService := service.NotificationService{}return notificationService
}func TestCreate(t *testing.T) {
mockings.Reset()
notification := entity.Notification{}
notificationService := GetNotificationService()
databaseWriterMock := DatabaseWriterMock{}
notificationService.SetDatabaseWriter(&databaseWriterMock)notificationService.Create(¬ification)
assert.NotEmpty(t, notification.GetGuid())
mockings.AssertCalledWith(t, &databaseWriterMock, "Insert", []interface{}{¬ification})
}func TestUpdateErrorMovingImage(t *testing.T) {
mockings.Reset()mime := make(map[string][]string, 0)
fileheader := multipart.FileHeader{}
fileheader.Filename = "bla"
fileheader.Header = mime
fileheader.Size = 1notification := entity.Notification{}
notification.SetId(1)
notification.SetUploadedImage(&fileheader)notificationService := GetNotificationService()
databaseWriterMock := DatabaseWriterMock{}
notificationService.SetDatabaseWriter(&databaseWriterMock)uploadProviderMock := UploadProviderMock{}
mockings.Mock(&uploadProviderMock, "Validate", []interface{}{&fileheader}, []interface{}{nil})
mockings.Mock(&uploadProviderMock, "GetExtension", []interface{}{&fileheader}, []interface{}{"jpg"})
mockings.Mock(&uploadProviderMock, "Move", mockings.ANY, []interface{}{errors.New("Bla")})
notificationService.SetUploadProvider(&uploadProviderMock)assert.Error(t, notificationService.Update(¬ification))
}
```