import ( "github.com/rafrombrc/gospec/src/gospec" ) func TestMyApp(t *testing.T) { c := gospec.NewContext(t) // Setup user and database connection before each test case c.BeforeEach(func() { user := createUser("testuser", "pass123") dbConn := connectToDatabase("testdb") c.Set("user", user) c.Set("dbConn", dbConn) }) // Define test cases c.Specify("Test user creation", func() { user := c.Get("user").(User) dbConn := c.Get("dbConn").(DatabaseConnection) err := user.create(dbConn) c.Expect(err, Equals, nil) }) })
import ( "net/http" "io/ioutil" "github.com/rafrombrc/gospec/src/gospec" ) func TestMyApp(t *testing.T) { c := gospec.NewContext(t) // Define test cases c.Specify("Test HTTP handler", func() { reqBody := "Hello world!" req, err := http.NewRequest("POST", "/some/path", strings.NewReader(reqBody)) c.Assume(err, Equals, nil) w := httptest.NewRecorder() handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Read request body body, err = ioutil.ReadAll(r.Body) c.Assume(err, Equals, nil) c.Expect(string(body), Equals, reqBody) }) handler.ServeHTTP(w, req) }) })In these examples, the `gospec` package is being used as a testing library to aid in the setup and assertion of test cases.