lundi 28 septembre 2020

Testing function that uses bcrypt

I'm testing function that uses bcrypt.GenerateFromPassword inside. Smth like:

func Foo(pass []byte) error {
    hash, err := bcrypt.GenerateFromPassword(pass, bcrypt.DefaultCost)
    if err != nil {
        return err
    }

    return repo.SetPassword(hash)
}

I was trying to test it like this:

pass := "foo"
hashedPass, _ := bcrypt.GenerateFromPassword(pass, bcrypt.DefaultCost)
repoMock.EXPECT().SetPassword(hashedPass).Times(1).Return(nil)
Foo(pass)

Obviously this test fails because of different salt :)
Any suggestions how to improve Foo or test, to make it work.

Aucun commentaire:

Enregistrer un commentaire