Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock your mocks - suggestions for follow up article with integration test sample #159

Open
surfmuggle opened this issue Dec 20, 2024 · 0 comments

Comments

@surfmuggle
Copy link

surfmuggle commented Dec 20, 2024

Thanks for the great article Mock your mocks.
In the article the function saveUser()

func saveUser(db *sql.DB, user User) error {
	if user.EmailAddress == "" {
		return errors.New("user requires an email")
	}
	if len(user.Password) < 8 {
		return errors.New("user password requires at least 8 characters")
	}
	hashedPassword, err = hash(user.Password)
	if err != nil {
		return err
	}
	_, err := db.Exec(`
		INSERT INTO users (password, email_address, created)
		VALUES ($1, $2, $3);`,
		hashedPassword, user.EmailAddress, time.Now(),
	)
	return err
}

is refactored; the validation of EmailAddress & Password were extracted into the function validateUser().

A follow up article on

  1. How the integration test for saveUser() could look like
  2. And how the two functions validateUser() and saveUser() are being called

would be great. I assume the code that calls both functions would like this

func AddNewUser(user User) error {
        errInvalidUser := validateUser(user)
        if errInvalidUser != nil {
           return errInvalidUser 
        }
        errSavingUser := saveUser(db, user)
        if errSavingUser != nil {
           return errSavingUser 
        }
	return nil
}

Assuming you have a webpage or a webservice that has an endpoint user/add the result of AddNewUser() should either be displayed on the page or send back to the caller. Adding this in the follow up article would be great as well.
Thanks
Best regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant