Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
165 lines (115 sloc) 3 KB
package main
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/mock"
)
func TestCreatePostFails(t *testing.T) {
db := new(fakeStore)
//dummy something
p := &post{
ID: 0,
Title: "me",
Slug: "oops-oops",
Content: "Used to be human",
}
db.On("Create", p).Return(errors.New("Something bad happened"))
defer db.AssertExpectations(t)
a := &app{db}
reader := strings.NewReader(`{"title":"me","content": "Used to be human", "slug": "oops-oops"}`)
r, _ := http.NewRequest(http.MethodGet, "/oops", reader)
defer r.Body.Close()
rr := httptest.NewRecorder()
http.HandlerFunc(createPost(a)).ServeHTTP(rr, r)
if got := rr.Code; got != http.StatusInternalServerError {
t.Fatalf(`Invalid status code.. Expected %d \n Got %d`,
http.StatusInternalServerError, got)
}
}
func TestCreatePost(t *testing.T) {
db := new(fakeStore)
//dummy something
p := &post{
ID: 0,
Title: "me",
Slug: "oops-oops",
Content: "Used to be human",
}
db.On("Create", p).Return(nil)
defer db.AssertExpectations(t)
a := &app{db}
reader := strings.NewReader(`{"title":"me","content": "Used to be human", "slug": "oops-oops"}`)
r, _ := http.NewRequest(http.MethodGet, "/oops", reader)
defer r.Body.Close()
rr := httptest.NewRecorder()
http.HandlerFunc(createPost(a)).ServeHTTP(rr, r)
if got := rr.Code; got != http.StatusOK {
t.Fatalf(`Invalid status code.. Expected %d \n Got %d`,
http.StatusOK, got)
}
}
func TestViewPost(t *testing.T) {
db := new(fakeStore)
p := post{
ID: 12,
Title: "me",
Slug: "oops-oops",
Content: "Used to be human",
}
db.On("FindByID", 12).Return(p, nil)
defer db.AssertExpectations(t)
a := &app{db}
r, _ := http.NewRequest(http.MethodGet, "/posts/view/12", strings.NewReader(""))
rr := httptest.NewRecorder()
http.HandlerFunc(viewPost(a)).ServeHTTP(rr, r)
if got := rr.Code; got != http.StatusOK {
t.Fatalf(`Invalid status code.. Expected %d \n Got %d`, http.StatusOK, got)
}
}
// fakeStore is an autogenerated mock type for the fakeStore type
//generated by the mockery tool
type fakeStore struct {
mock.Mock
}
// Create provides a mock function with given fields: p
func (_m *fakeStore) Create(p *post) error {
ret := _m.Called(p)
var r0 error
if rf, ok := ret.Get(0).(func(*post) error); ok {
r0 = rf(p)
} else {
r0 = ret.Error(0)
}
return r0
}
// Delete provides a mock function with given fields: ID
func (_m *fakeStore) Delete(ID int) error {
ret := _m.Called(ID)
var r0 error
if rf, ok := ret.Get(0).(func(int) error); ok {
r0 = rf(ID)
} else {
r0 = ret.Error(0)
}
return r0
}
// FindByID provides a mock function with given fields: ID
func (_m *fakeStore) FindByID(ID int) (post, error) {
ret := _m.Called(ID)
var r0 post
if rf, ok := ret.Get(0).(func(int) post); ok {
r0 = rf(ID)
} else {
r0 = ret.Get(0).(post)
}
var r1 error
if rf, ok := ret.Get(1).(func(int) error); ok {
r1 = rf(ID)
} else {
r1 = ret.Error(1)
}
return r0, r1
}