Answer the question
In order to leave comments, you need to log in
How to lock a struct method that is called in another method of the same struct in Golang?
Hello!
When writing tests, I ran into one problem. There is testable code;
Do() error
}
type MyInterface interface {
MethodA() error
MethodB() error
}
type MyStruct struct {
OtherStruct OtherInterface
}
func(s *MyStruct) MethodA() error {
return s.MethodB()
}
func(s *MyStruct) MethodB() error {
return s.OtherStruct.Do()
}
ype MockOtherStruct struct {
Do_Error error
}
func (t *MockOtherStruct) Do() error {return t.Do_Error}
TestMyStruct_MethodB(t *testing.B) {
expectedError = nil
mos := &MockOtherStruct{
Do_Error: nil
}
ms := MyStruct{
OtherStruct: mos,
}
err := ms.MethodB()
if err != expectedError {
t.Fatal(err)
}
}
Answer the question
In order to leave comments, you need to log in
No need to wet, you need to simplify the logic.
Also, *testing.B is used for benchmarks. You need to use *testing.T for testing.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question