Skip to content

Instantly share code, notes, and snippets.

@justinpage
Created August 5, 2020 01:14
Show Gist options
  • Select an option

  • Save justinpage/38559d8640cd32c6a2bd4006830835ba to your computer and use it in GitHub Desktop.

Select an option

Save justinpage/38559d8640cd32c6a2bd4006830835ba to your computer and use it in GitHub Desktop.
package service
import (
"testing"
)
type preCheckMock struct {
userExistsMock func(email string) bool
}
func (p preCheckMock) UserExists(email string) bool {
return p.userExistsMock(email)
}
func TestCheckUserExist(t *testing.T) {
user := User{
Name: "John Doe",
Email: "[email protected]",
UserName: "john",
}
err := RegisterUser(user, NewRegistrationPreChecker())
if err == nil {
t.Error("Expected Register User to throw an error, got nil")
}
}
func TestRegisterUser(t *testing.T) {
user := User{
Name: "John Doe",
Email: "[email protected]",
UserName: "john",
}
regPreCond := preCheckMock{func(email string) bool { return false }}
err := RegisterUser(user, regPreCond)
if err != nil {
t.Fatal(err)
}
regPreCond = preCheckMock{func(email string) bool { return true }}
err = RegisterUser(user, regPreCond)
if err == nil {
t.Error("Expected Register User to throw an error, got nil")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment