Giter VIP home page Giter VIP logo

mockings's Introduction

Mockings

package service

import "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(&notification)

    assert.NotEmpty(t, notification.GetGuid())

    mockings.AssertCalledWith(t, &databaseWriterMock, "Insert", []interface{}{&notification})
}

func TestUpdateErrorMovingImage(t *testing.T) {
    mockings.Reset()

    mime := make(map[string][]string, 0)

    fileheader := multipart.FileHeader{}
    fileheader.Filename = "bla"
    fileheader.Header = mime
    fileheader.Size = 1

    notification := 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(&notification))
}

mockings's People

Contributors

reisraff avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.