lundi 2 mars 2020

How to create a mock inside the function under test

I have a set of functions, which uses the pool of objects. This pool has been mocked. It works fine in most of the cases. But in some functions i call the methods of objects from the pool. So i need to mock this objects too.

Lets say:

// ObjectGeter is a interface that is mocked
type ObjectGeter interface {
    GetObject(id int) ObjectType, error
}

// this function is under test 
func SomeFunc(og ObjectGeter,id int, otherArgument SomeType) error {
    // some actions with otherArgument  
    // and may be return an error

    obj, err := og.GetObject(id)
    if err !=nil {
        return errors.New("GetObject error")
    }

    rezult, err := obj.SomeMethod()
    if err !=nil {
        return errors.New("One of internal errors")
    }

    return rezult, nil
}

Is there a way to test whole this function? I can create interface SomeMethoder which wraps the SomeMethod(), but i can't find the way how to assign it to obj inside SomeFunc without changing the signature of GetObject to GetObject(id int) SomeMethoder,error.

Currently i see the one approach - testing by a parts.

Aucun commentaire:

Enregistrer un commentaire