httpexpect icon indicating copy to clipboard operation
httpexpect copied to clipboard

Add environment in expect

Open zacscoding opened this issue 3 years ago • 0 comments

Add environment in Expect to manipulate test data.

This pr is a proposal for environment :) For examples, i want to use a value from previous test like below

func TestIT(t *testing.T) {
	e := httpexpect.New(t, "http://localhost:8080")

	t.Run("/users", func(t *testing.T) {
		obj := e.GET("/users").
			Expect().
			Status(http.StatusOK).JSON().Object()

		userID := obj.Path("$.users[1].id").String().Raw()
		e.Env.Put("user1.id", userID)
	})
	t.Run("/user/{userId}", func(t *testing.T) {
                // use a user's id from above tests.
		userID, err := e.Env.GetString("user1.id")
		assert.NoError(t, err)
		e.GET("/user/{userId}").
			WithPath("userId", userID)
			Expect().
			Status(http.StatusOK)
	})
}

If u agree with this concept, i will implement more such as int slice types with tests. Thanks for providing a good test library!

zacscoding avatar Aug 20 '21 11:08 zacscoding