Example #1
0
// TestCreateGame assumes that the database is faulty and gives out an error
func TestCreateGameWhenDBError(t *testing.T) {
	gin.SetMode(gin.ReleaseMode)
	db := mocks.DBInterface{}

	db.On("CreateGame", "Apocalypse World").Return(errors.New("Faulty DB"))

	r := router.Module{
		DB: db,
	}

	engine := r.Routes()

	// Legit Request
	body := bytes.NewBuffer([]byte("{\"title\":\"Apocalypse World\"}"))

	req, _ := http.NewRequest("POST", "http://localhost:8080/game", body)
	req.Header.Set("Content-Type", "application/json")

	resp := httptest.NewRecorder()

	engine.ServeHTTP(resp, req)

	assert.Equal(t, resp.Code, 500, " Legit Request should fail because of a faulty DB")
}
Example #2
0
// TestCreateGame assumes that the database is working and never giving errors
func TestCreateGame(t *testing.T) {
	gin.SetMode(gin.ReleaseMode)
	db := mocks.DBInterface{}

	db.On("CreateGame", "Apocalypse World").Return(nil)

	r := router.Module{
		DB: db,
	}

	engine := r.Routes()

	// Legit Request
	body := bytes.NewBuffer([]byte("{\"title\":\"Apocalypse World\"}"))

	req, _ := http.NewRequest("POST", "http://localhost:8080/game", body)
	req.Header.Set("Content-Type", "application/json")

	resp := httptest.NewRecorder()

	engine.ServeHTTP(resp, req)

	assert.Equal(t, resp.Code, 201, " Legit Request should be a success")

	// Bad Request
	body = bytes.NewBuffer([]byte("{\"garbage\":\"Look, no title\"}"))

	req, _ = http.NewRequest("POST", "http://localhost:8080/game", body)
	req.Header.Set("Content-Type", "application/json")

	resp = httptest.NewRecorder()

	engine.ServeHTTP(resp, req)

	assert.Equal(t, resp.Code, 400, "Bad Request should be a failure")
}