Unit testing 如何模拟/单元测试嵌套函数

Unit testing 如何模拟/单元测试嵌套函数,unit-testing,go,net-http,Unit Testing,Go,Net Http,我有一个函数,它在另一个函数中被调用 发送api.go function *send_api*(client *http.Client,url string) map[string]string,error { //send api request and parse the response and return the dict return dictmap for eg:{apple fruit} } 然后在main()函数中调用此函数 fun

我有一个函数,它在另一个函数中被调用

发送api.go

 function *send_api*(client *http.Client,url string) map[string]string,error {
    //send api request and parse the response and return the dict 

    return dictmap
    for eg:{apple fruit}       
}
然后在main()函数中调用此函数

func *main()*{

  getmap :=send_api(client *http.Client,"test.com")
 }
很好。走吧

func *get_dict_key*(key string) string,error {
   value,ok := get_map[key]
   if !ok {
          return fmt.Errorf("value is nil")
   }
   return value ,nil    
 }

function *good*(client *http.client, key string) {
 //get a dictionary value
 dcmap,err := get_dict_key("apple")
 if err != nil {
  panic(err)
  }
 value := dcmap[key]

 //use the value to do other processing
 }
单元测试

  func Test_good(t *testing.T) {
      Convey("AND quadra and conusl dcs are mapped",t, func() {
          mockResponses := send mock GET request to the URL and receive a response{"apple":"fruit"}
        }
        server, client := tools.TestClientServer(&mockResponses)
        defer server.Close()
        getMap := send_api(client.HTTPClient, "http://test")
        //At this point getMAP has value {'apple' 'fruit'}
        **q1.How to pass getMap value inside this get_dict_fkey function during testing?**
        value := get_dict_key("apple")
        good(client,"apple") #error:(value is nil)
问题1**q1.在测试期间,如何在这个get_dict_函数中传递getMap值*


任何指针都会有帮助?

假设您在模拟http.Client时遇到困难,我建议您选择以下选项

1。重构代码

您需要以这样一种方式重构代码,即您可以将可模拟的依赖项传递给要测试的函数

比如说,

重构
func send_api(client*http.client,url string)映射[string]string,error
,这样它就可以执行api请求并获取/解析数据,但可以从中调用另一个函数,该函数将执行进一步的处理(实际上您想要测试的是它,而不是http.client部分)

但是,使用这种方法,您可能无法测试端到端流。但是您可以单独测试这些函数

2。模拟http.Client

  func Test_good(t *testing.T) {
      Convey("AND quadra and conusl dcs are mapped",t, func() {
          mockResponses := send mock GET request to the URL and receive a response{"apple":"fruit"}
        }
        server, client := tools.TestClientServer(&mockResponses)
        defer server.Close()
        getMap := send_api(client.HTTPClient, "http://test")
        //At this point getMAP has value {'apple' 'fruit'}
        **q1.How to pass getMap value inside this get_dict_fkey function during testing?**
        value := get_dict_key("apple")
        good(client,"apple") #error:(value is nil)
同样,您可能需要重构代码。可以找到一些相关的文章


更新:建议观看

假设您面临模拟http.Client的困难,我想建议以下选项

1。重构代码

您需要以这样一种方式重构代码,即您可以将可模拟的依赖项传递给要测试的函数

比如说,

重构
func send_api(client*http.client,url string)映射[string]string,error
,这样它就可以执行api请求并获取/解析数据,但可以从中调用另一个函数,该函数将执行进一步的处理(实际上您想要测试的是它,而不是http.client部分)

但是,使用这种方法,您可能无法测试端到端流。但是您可以单独测试这些函数

2。模拟http.Client

  func Test_good(t *testing.T) {
      Convey("AND quadra and conusl dcs are mapped",t, func() {
          mockResponses := send mock GET request to the URL and receive a response{"apple":"fruit"}
        }
        server, client := tools.TestClientServer(&mockResponses)
        defer server.Close()
        getMap := send_api(client.HTTPClient, "http://test")
        //At this point getMAP has value {'apple' 'fruit'}
        **q1.How to pass getMap value inside this get_dict_fkey function during testing?**
        value := get_dict_key("apple")
        good(client,"apple") #error:(value is nil)
同样,您可能需要重构代码。可以找到一些相关的文章


更新:建议观看

不清楚您所说的“但不确定如何在good()函数中传递模拟值”是什么意思?@johnperayil我编辑了这个问题。希望现在已经清楚了我认为
sahaj
的回答给了您一些指导,如果您需要更具体的帮助,请告诉我,我会尽力为您解答。这些代码都无效-您能否提供一个至少可以编译的示例?不清楚“但不确定如何在good()函数中传递模拟值”是什么意思?@johnperayil我编辑了这个问题。希望现在问题清楚了我认为
sahaj
的答案会给你一些指导,如果你需要更具体的帮助,请告诉我,我会尽力为你解答。这些代码都无效-你能提供一个至少可以编译的示例吗?