[#3025] updated tests.ApiScenario fields

This commit is contained in:
Gani Georgiev
2023-08-25 22:14:04 +03:00
parent 4f3d1682de
commit 311bc74b7e
8 changed files with 74 additions and 51 deletions
+12 -11
View File
@@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
@@ -37,9 +38,9 @@ type ApiScenario struct {
// test hooks
// ---
TestAppFactory func() (*TestApp, error)
TestAppFactory func(t *testing.T) *TestApp
BeforeTestFunc func(t *testing.T, app *TestApp, e *echo.Echo)
AfterTestFunc func(t *testing.T, app *TestApp, e *echo.Echo)
AfterTestFunc func(t *testing.T, app *TestApp, res *http.Response)
}
// Test executes the test scenario.
@@ -54,16 +55,17 @@ func (scenario *ApiScenario) Test(t *testing.T) {
func (scenario *ApiScenario) test(t *testing.T) {
var testApp *TestApp
var testAppErr error
if scenario.TestAppFactory != nil {
// @todo consider passing the testing instance to the factory and maybe remove the error from the declaration
// (see https://github.com/pocketbase/pocketbase/discussions/3025)
testApp, testAppErr = scenario.TestAppFactory()
testApp = scenario.TestAppFactory(t)
if testApp == nil {
t.Fatal("TestAppFactory must return a non-nill app instance")
}
} else {
var testAppErr error
testApp, testAppErr = NewTestApp()
}
if testAppErr != nil {
t.Fatalf("Failed to initialize the test app instance: %v", testAppErr)
if testAppErr != nil {
t.Fatalf("Failed to initialize the test app instance: %v", testAppErr)
}
}
defer testApp.Cleanup()
@@ -172,8 +174,7 @@ func (scenario *ApiScenario) test(t *testing.T) {
}
}
// @todo consider adding the response body to the AfterTestFunc args
if scenario.AfterTestFunc != nil {
scenario.AfterTestFunc(t, testApp, e)
scenario.AfterTestFunc(t, testApp, res)
}
}