Skip to content

Commit

Permalink
add expected requests to tests; fix falsely passing Run() test
Browse files Browse the repository at this point in the history
  • Loading branch information
imsky committed Apr 25, 2019
1 parent 0640e3e commit 2c7f1c1
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type mockHTTPResponse struct {
body string
}

func (r mockHTTPResponse) String() string {
return r.method + " " + r.URL
}

func getResponse(responses []mockHTTPResponse, method string, url string) mockHTTPResponse {
for _, res := range responses {
if res.method == method && res.URL == url {
Expand Down Expand Up @@ -131,14 +135,16 @@ func TestListClosedPullRequests(t *testing.T) {
func TestRun(t *testing.T) {
now := time.Now().UTC()

expectedRequests := make([]string, 0, 1)

responses := []mockHTTPResponse{
mockHTTPResponse{
method: "GET",
URL: "/repos/user/repo/pulls?state=closed&sort=updated&direction=desc&per_page=100&page=1",
body: `[
{
"number": 1,
"closed_at": "` + now.Format(time.RFC3339) + `",
"updated_at": "` + now.Format(time.RFC3339) + `",
"head": {
"ref": "stalebranch",
"sha": "1761e021e70d29619ca270046b23bd243f652b98"
Expand Down Expand Up @@ -172,6 +178,8 @@ func TestRun(t *testing.T) {
t.Fatalf(r.URL.String())
}

expectedRequests = append(expectedRequests, res.String())

_, err := w.Write([]byte(res.body))

if err != nil {
Expand All @@ -191,6 +199,19 @@ func TestRun(t *testing.T) {
t.Errorf(err.Error())
}

for _, r := range responses {
found := false
for _, er := range expectedRequests {
if r.String() == er {
found = true
break
}
}
if !found {
t.Errorf("Expected request: " + r.String())
}
}

err = Run("", "repo", 1, *ex)

if err == nil {
Expand Down

0 comments on commit 2c7f1c1

Please sign in to comment.