Replies: 7 comments
-
Hello @gavv nice to see that you see the future of this library, but I will prefer that you shouldn't 'kill' the Builder pattern you have made so far, you can just put an interface with Response() or/and Request() or Object() to 'go back' to the previous builder, because the func(hr *hx.Response) doesn't seems to be a good idea for a testing library like this, you should target the ease-of-use |
Beta Was this translation helpful? Give feedback.
-
I absolutely love the current testing style. It's the main reason why we use this lib. Tests are fast and very readable. |
Beta Was this translation helpful? Give feedback.
-
If you do keep this new style for v2, consider creating type aliases for the functions. Then you can do:
Though you do have to know what the parameter name is, so making them all hp instead of hs/ho/hr might be better. |
Beta Was this translation helpful? Give feedback.
-
Note that the new API still uses the builder pattern. The only change is that it uses nested assertions for nested objects.
I think it'll make the user code too magical.
I understand that brevity is an important feature of httpexpect. However, clarity is probably even more important, and it's missing currently. Suggested API will preserve the current style as much as possible, with the only change of making nesting explicit. Of course, all existing shorthands that don't return nested objects can be used as usual, e.g. e.GET("/fruits/apple").
Expect(func (h *hx.Response) {
h.Status(http.StatusOK)
h.JSON(func(h *hx.Object) {
h.ContainsKey("foo").ValueEqual("foo", 1)
h.ContainsKey("bar").ValueEqual("bar", 2)
})
}) I'm still experimenting with various ideas, but so far the API suggested above seems to be the best compromise if you want short, but non-magical tests.
Looks interesting, but I don't like that it hides the types again. I think users that need this can add such aliases by themselves.
Anyway, even without such aliases it seems to be a good idea to use the same identifier for all nesting levels. I'll use it in new documentation and examples. |
Beta Was this translation helpful? Give feedback.
-
@gavv I totally feel you, it is impossible to change your mind right now, if you have an idea, just do it, I'll still support and use this library :) |
Beta Was this translation helpful? Give feedback.
-
I just can support mattes and ghost's comment. The old way looks much more readable. Maybe you could add methods like AND() so you can go back from an expect chain and do another expect like: Json().Contains("abc").Contains("def").And().Status(200)? |
Beta Was this translation helpful? Give feedback.
-
Well, no one supported the idea of the new API for quite a long time, so I think I should not break compatibility for solving problems that are not critical for real users. If this will ever happen, it's likely should be a separate library. Closing this for now :) |
Beta Was this translation helpful? Give feedback.
-
Problems with current API
The big problem with current API is that it's totally unclear from the source code what is the type of every object in a chained call.
Here is an example:
Actually there are two kind of methods: ones that return the method receiver, and ones that return a new object for a nested value. However, it's hard to recognize what is the kind of a given method, and the user should consult the documentation or IDE for each one.
This makes the code unclear for those who are unfamiliar with the httpexpect API. People already mentioned this on reddit when httpexpect.v1 was announced there.
Suggested new API
I finally came up to an idea (actually not a new one) of the new API that will solve the problem. The new suggested API is based on lambdas. Less talk, more code. Here is the above example ported to new API:
The same may be written as:
Or:
The new rules are dead simple:
Advantages:
Disadvantages:
Since the new API forces to use package name much more often, it will be changed from
httpexpect
tohx
(github.com/gavv/httpexpect/hx
).Migration process
The
httpexpect.v1
stable version (available on gopkg) will not be dropped and the compatibility will be preserved. I will not develop new features for it, but I will fix bugs and accepts pull requests.The
master
will be switched to a new API when it will be ready and usable. I didn't start working on the new API yet, so it will take some time.To allow incremental migration, every object in the new API will provide
Compat()
method returning an appropriate instance from the old API. Thus you can still use parts of code written for the old API.When the new API will be stabilized, the
Compat()
methods will be removed and thehttpexpect.v2
stable branch will be created.If you're currently using master (
github.com/gavv/httpexpect
), it's time to switch to a stable branch (gopkg.in/gavv/httpexpect.v1
) to avoid breaks in near future.Other (minor) breaking changes in v2
Looking for feedback
Before starting working on the new API, I'm looking for any feedback and suggestions. You're welcome!
@acidvertigo @banux @EtienneBruines @gosilent @JamesMura @joohoi @jraede @kataras @lstep @mattes @mrLSD @nullstyle @oalmali @phifty @sofixa @typekpb @yudaiBeta Was this translation helpful? Give feedback.
All reactions