Last active
April 20, 2017 22:22
-
-
Save crissilvaeng/2a9bab43470750d974c7a3ee94614183 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TestGetAnswer(t *testing.T) { | |
httpmock.Activate() | |
defer httpmock.DeactivateAndReset() | |
httpmock.RegisterResponder("GET", "https://localhost:8080/answer", | |
httpmock.NewStringResponder(200, `{"answer": 42}`)) | |
} | |
func TestPostAnswer(t *testing.T) { | |
httpmock.Activate() | |
defer httpmock.DeactivateAndReset() | |
httpmock.RegisterResponder("POST", affiliationUrl, | |
httpmock.NewStringResponder(201, `{"answer": 42}`)) | |
} | |
func TestMockingUniverse(t *testing.T) { | |
ctrl := gomock.NewController(t) | |
providerUniverse := mock.NewMockUniverse(ctrl) | |
defer ctrl.Finish() | |
providerUniverse.GiveMeTheAnswer().Return(42, nil) | |
universe.SetProvider(providerUniverse) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package universe | |
import "github.com/crissilvaeng/goseed/api/universe" | |
var provider Universe | |
func init() { | |
provider = new(universe) | |
} | |
func Provider() Universe { | |
return provider | |
} | |
func SetProvider(universe Universe) { | |
provider = universe | |
} | |
type Universe interface { | |
GiveMeTheAnswer() (int, error) | |
} | |
type universe struct{} | |
func (u *universe) GiveMeTheAnswer() (int, error) { | |
return u.Answer() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package math | |
import "testing" | |
type testpair struct { | |
values []float64 | |
average float64 | |
} | |
var tests = []testpair{ | |
{ []float64{1,2}, 1.5 }, | |
{ []float64{1,1,1,1,1,1}, 1 }, | |
{ []float64{-1,1}, 0 }, | |
} | |
func TestAverage(t *testing.T) { | |
for _, pair := range tests { | |
v := Average(pair.values) | |
if v != pair.average { | |
t.Error( | |
"For", pair.values, | |
"expected", pair.average, | |
"got", v, | |
) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
λ go test -v | |
=== RUN TestAnswers | |
=== RUN TestAnswers/Answers=42 | |
=== RUN TestAnswers/Answers=Batata | |
--- PASS: TestAnswers (0.00s) | |
--- PASS: TestAnswers/Answers=42 (0.00s) | |
--- PASS: TestAnswers/Answers=Batata (0.00s) | |
PASS | |
ok github.com/crissilvaeng/goseed/api/universe 0.026s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package universe | |
// Answer to life, the universe and everything. | |
func Answer() int { | |
return 42 | |
} | |
// OtherAnswer to life, the universe and everything. | |
func OtherAnswer() string { | |
return "batata" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package universe | |
import "testing" | |
func TestAnswers(t *testing.T) { | |
t.Run("Answers=42", func(t *testing.T) { | |
expected := 42 | |
actual := Answer() | |
if expected != actual { | |
t.Errorf("The anwser should be %d and got %d.", expected, actual) | |
} | |
}) | |
t.Run("Answers=Batata", func(t *testing.T) { | |
expected := "batata" | |
actual := OtherAnswer() | |
if expected != actual { | |
t.Errorf("The anwser should be %s and got %s.", expected, actual) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment