Skip to content

Commit

Permalink
ybkuroki#18 Implement unit test for master controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ybkuroki committed Jun 21, 2020
1 parent a68ed98 commit 965d515
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions controller/mater_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package controller

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/ybkuroki/go-webapp-sample/model"
"github.com/ybkuroki/go-webapp-sample/test"
)

func TestGetCategoryList(t *testing.T) {
router := test.Prepare()
router.GET("/api/master/category", GetCategoryList())

req := httptest.NewRequest("GET", "/api/master/category", nil)
rec := httptest.NewRecorder()

router.ServeHTTP(rec, req)

data := [...]*model.Category{
&model.Category{ID: 1, Name: "技術書"},
&model.Category{ID: 2, Name: "雑誌"},
&model.Category{ID: 3, Name: "小説"},
}

assert.Equal(t, http.StatusOK, rec.Code)
assert.JSONEq(t, test.ConvertToString(data), rec.Body.String())
}

func TestGetFormatList(t *testing.T) {
router := test.Prepare()
router.GET("/api/master/format", GetFormatList())

req := httptest.NewRequest("GET", "/api/master/format", nil)
rec := httptest.NewRecorder()

router.ServeHTTP(rec, req)

data := [...]*model.Format{
&model.Format{ID: 1, Name: "書籍"},
&model.Format{ID: 2, Name: "電子書籍"},
}

assert.Equal(t, http.StatusOK, rec.Code)
assert.JSONEq(t, test.ConvertToString(data), rec.Body.String())
}

0 comments on commit 965d515

Please sign in to comment.