Skip to content

Instantly share code, notes, and snippets.

@kpurdon
Created April 24, 2016 22:18
Show Gist options
  • Select an option

  • Save kpurdon/ead53e2fdec60d9766ba9c9da753b5fa to your computer and use it in GitHub Desktop.

Select an option

Save kpurdon/ead53e2fdec60d9766ba9c9da753b5fa to your computer and use it in GitHub Desktop.

Revisions

  1. kpurdon revised this gist Apr 24, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion TestGet
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ func TestGet(t *testing.T) {
    expectedRepos: []Repo(nil),
    expectedError: errors.New("github api: no results found"),
    },
    // TODO: not all cases are tested, but this is enough of a sample
    // not all cases are tested, but this is enough of a sample
    }

    for _, tc := range tests {
  2. kpurdon created this gist Apr 24, 2016.
    44 changes: 44 additions & 0 deletions TestGet
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    var c = ReposClient{}

    func TestGet(t *testing.T) {
    assert := assert.New(t)

    httpmock.Activate()
    defer httpmock.DeactivateAndReset()

    tests := []struct {
    description string
    responder httpmock.Responder
    expectedRepos []Repo
    expectedError error
    }{
    {
    description: "github api success",
    responder: httpmock.NewStringResponder(200, `[{"name": "test", "description": "a test"}]`),
    expectedRepos: []Repo{Repo{Name: "test", Description: "a test"}},
    expectedError: nil,
    }, {
    description: "github api success, no repos",
    responder: httpmock.NewStringResponder(200, `[]`),
    expectedRepos: []Repo{},
    expectedError: nil,
    }, {
    description: "github api failure, not found",
    responder: httpmock.NewStringResponder(404, `{"message": "not found"}`),
    expectedRepos: []Repo(nil),
    expectedError: errors.New("github api: no results found"),
    },
    // TODO: not all cases are tested, but this is enough of a sample
    }

    for _, tc := range tests {
    httpmock.RegisterResponder("GET", "https://api.github.com/users/fake/repos", tc.responder)

    r, err := c.Get("fake")

    assert.Equal(r, tc.expectedRepos, tc.description)
    assert.Equal(err, tc.expectedError, tc.description)

    httpmock.Reset()
    }
    }