假设我正在使用TDD来创建一个A类.在我完成并有一个"绿色"栏后,我决定使用一些重构工具从A类中仅使用静态方法提取一些B类.我现在已经对A类和B类进行了全面的单元测试,但只通过A类的测试类.我现在还应该创建一个特定于B类功能的测试类,即使这会重复测试吗?
我有一个 Golang 程序,设置了上下文截止日期。我正在发送一个 HTTP 请求,并希望在阅读正文时看到超出截止日期的错误。
似乎当我使用ioutil.ReadAllthen 读取响应正文时,该读取方法将被中断(?)并返回适当的错误(context.DeadlineExceeded)。
但是,如果我读取响应正文,json.NewDecoder(resp.Body).Decode则返回的错误为 nil (而不是context.DeadlineExceeded)。我的完整代码如下。这是一个错误吗json.NewDecoder(resp.Body).Decode?
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
var url string = "http://ip.jsontest.com/"
func main() {
readDoesntFail()
readFails()
}
type IpResponse struct {
Ip string
}
func readDoesntFail() {
ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
panic(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil { …Run Code Online (Sandbox Code Playgroud)