简体   繁体   中英

Mock functions in golang for testing

I want to write unittest case for CreateData() function, by mocking FetchAllData() and SaveData() which are in someother package(package2), please help me to mock the function with example,

Thanks in advance


func CreateData(input package1.InputRequest) (output package1.OututResponse){

    ..some code
    ..some code

    DBdata, err := package2.FetchAllData() //function to fetch data from database

    ..some code
    ..some code

    id, insertErr := package2.SaveData(someData) //function to insert data

    ..some code
    ..some code
}

I need to just mock the above function without modifying the function. I'm looking for that

Make use of the interface feature. It may require some change to the dependencies (in this example, the package2 ).

Change your code to something like this (the point is, to define several interfaces for better testability):

type Fetcher interface {
    FetchAllData() ([]Data, error)
}

type Saver interface {
    SaveData([]Data) (int, error)
}


func CreateData(input package1.InputRequest) (output package1.OututResponse){

    ..some code
    ..some code

    // when you are writing tests, replace it with `s := NewMocking()`
    s := package2.NewStorage()

    DBdata, err := s.FetchAllData() //function to fetch data from database

    ..some code
    ..some code

    id, insertErr := s.SaveData(someData) //function to insert data

    ..some code
    ..some code
}

A little change to the package2 is needed:

type MyStorage struct {}

func (s *MyStorage) FetchAllData ([]Data, error) {
    // ... some fetching code
}

func (s *MyStorage) SaveData(someData []Data) (int, error) {
    // ... some saving code
}

func NewStorage() *MyStorage {
    // ... some initialization
}

When you want to do some tests without a requirement of package2 , define your own mocking type:

type Mocking struct {}

func (s *Mocking) FetchAllData ([]Data, error) {
    // ... some mocking logic
}

func (s *Mocking) SaveData(someData []Data) (int, error) {
    // ... some mocking logic
}

func NewMocking() *Mocking {
    // ... initialize ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM