使用类型参数测试json.Marshal(myType)。

huangapple go评论108阅读模式
英文:

Testing json.Marshal(myType) with typed parameter

问题

我有这个方法

  1. func getRequestBody(object MyType) ([]byte, error) {
  2. if object == nil {
  3. return nil, nil
  4. }
  5. requestBody, err := json.Marshal(object)
  6. if err != nil {
  7. return nil, errors.New("无法将请求体编组为JSON")
  8. }
  9. return requestBody, nil
  10. }

我正在尝试像这样测试该函数:

  1. t.Run("errorMarshaling", func(t *testing.T) {
  2. body, err := getRequestBody([某个无法编组的MyType对象])
  3. assert.Nil(t, body)
  4. assert.Equal(t, err.Error(), "无法将请求体编组为JSON")
  5. })

有没有办法在不更改函数属性类型的情况下完成这个测试?

英文:

I have this method

  1. func getRequestBody(object MyType ) ([]byte, error) {
  2. if object == nil {
  3. return nil, nil
  4. }
  5. requestBody, err := json.Marshal(object)
  6. if err != nil {
  7. return nil, errors.New("unable to marshal the request body to json")
  8. }
  9. return requestBody, nil
  10. }

And I'm trying to test the function like this:

  1. t.Run("errorMarshaling", func(t *testing.T) {
  2. body, err := getRequestBody([some object of MyType to do the marshal fail])
  3. assert.Nil(t, body)
  4. assert.Equal(t, err.Error(), "unable to marshal the request body to json")
  5. })

Is there any way to do it without changing the function attribute type?

答案1

得分: 2

json.marshall只会在文档中列出的特定情况下抛出错误:

> 无法将通道、复杂类型和函数值编码为JSON。
> 尝试编码此类值会导致Marshal返回UnsupportedTypeError。
>
> JSON无法表示循环数据结构,Marshal也无法处理它们。将循环结构传递给Marshal将导致错误。

如果底层对象无法满足这些条件之一,我认为您将无法使其在测试中失败。

英文:

json.marshall will only throw an error in specific circumstances listed in the documentation:

> Channel, complex, and function values cannot be encoded in JSON.
> Attempting to encode such a value causes Marshal to return an
> UnsupportedTypeError.
>
> JSON cannot represent cyclic data structures and Marshal does not
> handle them. Passing cyclic structures to Marshal will result in an
> error.

If the underlying object doesn't have the ability to meet one of these criteria, I don't think you'll be able to get it to fail in your test.

答案2

得分: 0

最后我改变了我的函数参数

  1. type myTypeBody interface{}
  2. func getRequestBody(object myTypeBody) ([]byte, error) {
  3. if object == nil {
  4. return nil, nil
  5. }
  6. requestBody, err := json.Marshal(object)
  7. if err != nil {
  8. return nil, errors.New("无法将请求体编组为 JSON")
  9. }
  10. return requestBody, nil
  11. }

测试部分如下:

  1. t.Run("errorMarshaling", func(t *testing.T) {
  2. body, err := getRequestBody(func() {})
  3. assert.Nil(t, body)
  4. assert.Equal(t, err.Error(), "无法将请求体编组为 JSON")
  5. })
英文:

Finally I changed my function parameter

  1. type myTypeBody interface{}
  2. func getRequestBody(object myTypeBody ) ([]byte, error) {
  3. if object == nil {
  4. return nil, nil
  5. }
  6. requestBody, err := json.Marshal(object)
  7. if err != nil {
  8. return nil, errors.New("unable to marshal the request body to json")
  9. }
  10. return requestBody, nil
  11. }

And the test

  1. t.Run("errorMarshaling", func(t *testing.T) {
  2. body, err := getRequestBody(func() {})
  3. assert.Nil(t, body)
  4. assert.Equal(t, err.Error(), "unable to marshal the request body to json")
  5. })

huangapple
  • 本文由 发表于 2022年4月4日 20:21:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/71737178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定