package mainimport ( "net/http" "sync" "time")type SessionInterface1 interface { doLoginAndReadDestinations1() bool}type Session1 struct { sessionCookie string mux sync.Mutex sessionTime time.Time targetAddress string currentJwt string transport *http.Transport}var currentSession1 Session1func main() { currentSession1.verifyLogin1()}func (s *Session1) doLoginAndReadDestinations1() bool { ..logic... ... for example return true}func callDest1(si SessionInterface1) bool { return si.doLoginAndReadDestinations1()}func (s *Session1) verifyLogin1() bool { return callDest1(s)}我想創(chuàng)建單元測(cè)試并模擬 doLoginAndReadDestinations1 我嘗試為此方法創(chuàng)建接口并創(chuàng)建測(cè)試func test1(t *testing.T) { type args struct { } tests := []struct { name string args args want bool }{{ name: "test", args: args{}, want: false, }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var s1 *Session1 count := 10 s1 = &Session1{"", sync.Mutex{}, time.Now(), "", "", nil} var got1 = s1.verifyLogin() if got1 != tt.want { t.Errorf("getJwt() = %v, want %v", got, tt.want) } }) }}我不知道如何在測(cè)試中更改函數(shù) doLoginAndReadDestinations1 的邏輯并更改第一個(gè) main 中的邏輯
如何在 go 中模擬函數(shù)?
三國(guó)紛爭(zhēng)
2023-05-04 17:23:14