Skip to content

Instantly share code, notes, and snippets.

@quii
Last active May 13, 2020 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quii/96c49d33715d3eb55006c3c8852ff11d to your computer and use it in GitHub Desktop.
Save quii/96c49d33715d3eb55006c3c8852ff11d to your computer and use it in GitHub Desktop.
Why does this test pass huh?
package learn_go_with_tests
import (
"fmt"
"testing"
)
func Add(x, y int) int {
return 3
}
// Why does this test pass given the implementation of Add?
func TestAdd(t *testing.T) {
cases := []struct{
X, Y int
Want int
} {
{X: 1, Y: 1, Want: 2},
{X: 1, Y: 2, Want: 3},
}
for _, test := range cases {
t.Run(fmt.Sprintf("%d plus %d is %d", test.X, test.Y, test.Want), func(t *testing.T) {
t.Parallel()
got := Add(test.X, test.Y)
if got != test.Want{
t.Errorf("got %d, wanted %d", got, test.Want)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment