drnxnbf199049 2017-03-17 14:46 采纳率: 100%
浏览 40

Golang Godog REST API测试失败

For past 2 weeks I have been looking into GODOG, a cucumber like bdd for golang. I found it very interesting and recently I am spending more time on writing a test for my REST API(s). Recently, I am continiuosly fialing to pass one of my test. This one includes a JSON struct that has childen JSON(s) inside itself. Also I am following exact example found at following link for my test:

https://github.com/DATA-DOG/godog/tree/master/examples/api

I have a struct like:

type Status struct {
    ErrorCode   string  `json:"ERROR_CODE"`
    ErrorText   string  `json:"ERROR_TEXT"`
}

type OutputResponse1 struct {
    Status Status `json:"STATUS"`
}

type OutputResponse2 struct {
    Status Status `json:"STATUS"`
    Config json.RawMessage `json:"CONFIG"`
}

A byte type variable:

var responseByte []byte

And two different kind of output response:

//--------------1

responseByte, _ = json.Marshal(OutputResponse1{
    Status: Status{
        ErrorCode: "-2",
        ErrorText:  "Config was not found",
    },
})

//------------2
responseByte, _ = json.Marshal(&OutputResponseSuccess{
        Status: Status{
            ErrorCode: "0",
            ErrorText:  " ",},
        Config: json.RawMessage(body),
})

The json.Rawmessage is something coming from another source which looks like:

 {
      "binaryVersion":"1.0.0",
      "configVersion":"1.1.1"
 }

Now in feature file I have tested out something like this:

 //--------------1

 And the response should match json:

    {
      "STATUS": {
         "ERROR_CODE": "-2",
         "ERROR_TEXT": "Config was not found"
      }
    }

THIS TEST PASS

The second one being one with json.RawMessage

And the response should match json:

 {
      "STATUS": {
          "ERROR_CODE":"0",
          "ERROR_TEXT":" "
      },
      "CONFIG":{
          "binaryVersion":"1.0.0",
          "configVersion":"1.1.1"
      }
 }

NOW THIS ONE FAILS, EVEN THOUGH THE GODG OUTPUT HAS THE ACTUAL AND EXPECTED SAME TO SAME

------------------------Acutal output of godog test---------------------------

 And the response should match json:


 {"STATUS":{"ERROR_CODE":"0","ERROR_TEXT":" "},"CONFIG":      {"binaryVersion":"1.0.0","configVersion":"1.1.1"}}


 Expected json does not match actual:
 {"STATUS":{"ERROR_CODE":"0","ERROR_TEXT":" "},"CONFIG":     {"binaryVersion":"1.0.0","configVersion":"1.1.1"}}

 --- Failed scenarios:

Someone from the godog also advised me to get rid of leading and trailing whitespace, so I even did

bytes.TrimSpace(responseByte)

still no luck.

Anyone facing the same problem?

  • 写回答

2条回答 默认 最新

  • donglu953744 2017-03-17 16:21
    关注

    Not sure if this will solve your issue, but if you're on Go1.7 or less then try using *json.RawMessage as the Config field's type. Because Go's pre 1.8 json.RawMessage had its MarshaJSON method defined on the pointer receiver as opposed to the value receiver which caused json.RawMessage values to be encoded as strings bytes in base64.

    type OutputResponse2 struct {
        Status Status `json:"STATUS"`
        Config *json.RawMessage `json:"CONFIG"`
    }
    
    raw := json.RawMessage(body)
    responseByte, err := json.Marshal(&OutputResponseSuccess{
            Status: Status{
                ErrorCode: "0",
                ErrorText:  " ",
            },
            Config: &raw,
    })
    if err != nil {
        // ...
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器