mardi 5 juillet 2016

How to test this simple method with Go?

I'm writing some unit tests, and I'm stuck writing a test for the following method:

func (database *Database) FindUnusedKey() string {
    count := 0
    possibleKey := helpers.RandomString(helpers.Config.KeySize)
    for database.DoesKeyExist(possibleKey) {
        possibleKey = helpers.RandomString(helpers.Config.KeySize + uint8(count/10))
        count++
    }
    return possibleKey
}

I want a test in which helpers.RandomString(int) returns a string that is already a key in my database, but I've found no way to redeclare or monkey patch helpers.RandomString(int) in my test.

I tried using testify mock, but it doesn't seem possible.

Am I doing something wrong?

Thanks.

Aucun commentaire:

Enregistrer un commentaire