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

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

Testing json.Marshal(myType) with typed parameter

问题

我有这个方法

func getRequestBody(object MyType) ([]byte, error) {
    if object == nil {
        return nil, nil
    }

    requestBody, err := json.Marshal(object)
    if err != nil {
        return nil, errors.New("无法将请求体编组为JSON")
    }
    return requestBody, nil
}

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

t.Run("errorMarshaling", func(t *testing.T) {

    body, err := getRequestBody([某个无法编组的MyType对象])

    assert.Nil(t, body)
    assert.Equal(t, err.Error(), "无法将请求体编组为JSON")
})

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

英文:

I have this method

func getRequestBody(object MyType ) ([]byte, error) {
	if object == nil {
		return nil, nil
	}

	requestBody, err := json.Marshal(object)
	if err != nil {
		return nil, errors.New("unable to marshal the request body to json")
	}
	return requestBody, nil
}

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

	t.Run("errorMarshaling", func(t *testing.T) {

		body, err := getRequestBody([some object of MyType to do the marshal fail])

		assert.Nil(t, body)
		assert.Equal(t, err.Error(), "unable to marshal the request body to json")
	})

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

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

type myTypeBody interface{}

func getRequestBody(object myTypeBody) ([]byte, error) {
    if object == nil {
        return nil, nil
    }

    requestBody, err := json.Marshal(object)
    if err != nil {
        return nil, errors.New("无法将请求体编组为 JSON")
    }
    return requestBody, nil
}

测试部分如下:

t.Run("errorMarshaling", func(t *testing.T) {

    body, err := getRequestBody(func() {})

    assert.Nil(t, body)
    assert.Equal(t, err.Error(), "无法将请求体编组为 JSON")
})
英文:

Finally I changed my function parameter

type myTypeBody interface{}

func getRequestBody(object myTypeBody ) ([]byte, error) {
    if object == nil {
        return nil, nil
    }

    requestBody, err := json.Marshal(object)
    if err != nil {
        return nil, errors.New("unable to marshal the request body to json")
    }
    return requestBody, nil
}

And the test

t.Run("errorMarshaling", func(t *testing.T) {

        body, err := getRequestBody(func() {})

        assert.Nil(t, body)
        assert.Equal(t, err.Error(), "unable to marshal the request body to json")
    })

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:

确定