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 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'